<br><br><div class="gmail_quote">2012/7/6 Joel Sherrill <span dir="ltr"><<a href="mailto:joel.sherrill@oarcorp.com" target="_blank">joel.sherrill@oarcorp.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On 07/06/2012 04:49 AM, Ashi wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi, all.<br>
I've a problem of _Thread_Disable_dispatch() function in below code. It's my implementation of _POSIX_Keys_Run_destructors() function. I come across the problem when I run the psxkey03 test:if _Thread_Disable_dispatch() was commented out, psxkey03 can run without problem. when _Thread_Disable_dispatch() added at line 1(as below), psxkey03 will fail. I debug it in sparc-rtems4.11-gdb, and find psxkey03 runs into _Internal_error_Occurred() function then stop. I know _POSIX_Keys_Get()(at line 12) will also disable thread dispatch--does this lead to the problem? However, can't figure out where exactly the problem exists, could anyone explain it? Thanks!<br>

</blockquote>
<br></div>
This call chain goes back to score/src/threadclose.c and this section of code.<br>
<br>
  /*<br>
   *  We assume the Allocator Mutex is locked when we get here.<br>
   *  This provides sufficient protection to let the user extensions<br>
   *  run but as soon as we get back, we will make the thread<br>
   *  disappear and set a transient state on it.  So we temporarily<br>
   *  unnest dispatching.<br>
   */<br>
  _Thread_Unnest_dispatch();<br>
<br>
  _User_extensions_Thread_<u></u>delete( the_thread );<br>
<br>
 **** you are called in this state ***<br>
  - mutex allocator locked<br>
  - dispatching disabled (level = 1)<div class="im"><br></div></blockquote><div>I find the mutex allocator's work is protection for all memory allocations and deallocations, does it mean I can't call _Workspace_Free at line 17?<br>
</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="im">
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
  1 _Thread_Disable_dispatch();<br>
<br>
  2 chain = &((POSIX_API_Control *)thread->API_Extensions[ THREAD_API_POSIX ])->the_chain;<br>
  3 iter = _Chain_First( chain );<br>
  4 while ( !_Chain_Is_tail( chain, iter ) ) {<br>
  5 next = _Chain_Next( iter );<br>
  6  /**<br>
  7   * here Chain_Node *iter can be convert to POSIX_Keys_Rbtree_node *,<br>
  8   * because Chain_Node is the first member of POSIX_Keys_Rbtree_node structure.<br>
  9   */<br>
  10  _RBTree_Extract_unprotected( &_POSIX_Keys_Rbtree, &((POSIX_Keys_Rbtree_node *)iter)->rb_node );<br>
  11 _Chain_Extract_unprotected( iter );<br>
<br>
</blockquote></div>
Thread dispatching is disabled here to level 1.<div class="im"><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  12  the_key = _POSIX_Keys_Get( ((POSIX_Keys_Rbtree_node *)iter)->key, &location);<br>
</blockquote></div>
This nests the dispatching level to 2.<br>
 <br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
I would move your code below the _POSIX_Keys_Get() call. There are logically<br>
two actions: key removal from rbtree and run the destructor. Put comments<br>
above each block of code indicating what is happening LOGICALLY. Everyone<br>
can read the code.<br>
<br>
If order is important between the two blocks, document it. I am assuming your<br>
block will move between lines 12 and 13.<div class="HOEnZb"><div class="h5"><br></div></div></blockquote><div>Sorry, Joel, I haven't quite got your idea, what do you mean by "move code below the _POSIX_Keys_Get() call"? <br>
<br>However, I test the code after add a _Thread_Enable_dispatch() between line 16 and 17, and the error is gone. Is it a proper solution to my problem?<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div class="HOEnZb"><div class="h5">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  13  destructor = the_key->destructor;<br>
  14  value = ((POSIX_Keys_Rbtree_node *)iter)->value;<br>
  15  if ( destructor != NULL && value != NULL )<br>
  16    (*destructor)( value );<br>
<br>
  17  _Workspace_Free( (POSIX_Keys_Rbtree_node *)iter );<br>
  18  iter = next;<br>
  19}<br>
<br>
  20_Thread_Enable_dispatch();<br>
<br>
---<br>
Best regards!<br>
Zhongwei Yao<br>
<br>
</blockquote>
<br>
<br></div></div><span class="HOEnZb"><font color="#888888">
-- <br>
Joel Sherrill, Ph.D.             Director of Research&   Development<br>
joel.sherrill@OARcorp.com        On-Line Applications Research<br>
Ask me about RTEMS: a free RTOS  Huntsville AL 35805<br>
    Support Available             <a href="tel:%28256%29%20722-9985" value="+12567229985" target="_blank">(256) 722-9985</a><br>
<br>
<br>
</font></span></blockquote></div><br><br clear="all"><br>-- <br>Best wishes!<br>Zhongwei Yao<br><br>