mutex_os2.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. ** 2007 August 28
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. ** This file contains the C functions that implement mutexes for OS/2
  13. **
  14. ** $Id: mutex_os2.c,v 1.3 2007/10/02 19:56:04 pweilbacher Exp $
  15. */
  16. #include "sqliteInt.h"
  17. /*
  18. ** The code in this file is only used if SQLITE_MUTEX_OS2 is defined.
  19. ** See the mutex.h file for details.
  20. */
  21. #ifdef SQLITE_MUTEX_OS2
  22. /********************** OS/2 Mutex Implementation **********************
  23. **
  24. ** This implementation of mutexes is built using the OS/2 API.
  25. */
  26. /*
  27. ** The mutex object
  28. ** Each recursive mutex is an instance of the following structure.
  29. */
  30. struct sqlite3_mutex {
  31. PSZ mutexName; /* Mutex name controlling the lock */
  32. HMTX mutex; /* Mutex controlling the lock */
  33. int id; /* Mutex type */
  34. int nRef; /* Number of references */
  35. TID owner; /* Thread holding this mutex */
  36. };
  37. /*
  38. ** The sqlite3_mutex_alloc() routine allocates a new
  39. ** mutex and returns a pointer to it. If it returns NULL
  40. ** that means that a mutex could not be allocated.
  41. ** SQLite will unwind its stack and return an error. The argument
  42. ** to sqlite3_mutex_alloc() is one of these integer constants:
  43. **
  44. ** <ul>
  45. ** <li> SQLITE_MUTEX_FAST 0
  46. ** <li> SQLITE_MUTEX_RECURSIVE 1
  47. ** <li> SQLITE_MUTEX_STATIC_MASTER 2
  48. ** <li> SQLITE_MUTEX_STATIC_MEM 3
  49. ** <li> SQLITE_MUTEX_STATIC_PRNG 4
  50. ** </ul>
  51. **
  52. ** The first two constants cause sqlite3_mutex_alloc() to create
  53. ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
  54. ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
  55. ** The mutex implementation does not need to make a distinction
  56. ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
  57. ** not want to. But SQLite will only request a recursive mutex in
  58. ** cases where it really needs one. If a faster non-recursive mutex
  59. ** implementation is available on the host platform, the mutex subsystem
  60. ** might return such a mutex in response to SQLITE_MUTEX_FAST.
  61. **
  62. ** The other allowed parameters to sqlite3_mutex_alloc() each return
  63. ** a pointer to a static preexisting mutex. Three static mutexes are
  64. ** used by the current version of SQLite. Future versions of SQLite
  65. ** may add additional static mutexes. Static mutexes are for internal
  66. ** use by SQLite only. Applications that use SQLite mutexes should
  67. ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
  68. ** SQLITE_MUTEX_RECURSIVE.
  69. **
  70. ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
  71. ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
  72. ** returns a different mutex on every call. But for the static
  73. ** mutex types, the same mutex is returned on every call that has
  74. ** the same type number.
  75. */
  76. sqlite3_mutex *sqlite3_mutex_alloc(int iType){
  77. PSZ mutex_name = "\\SEM32\\SQLITE\\MUTEX";
  78. int mutex_name_len = strlen(mutex_name) + 1; /* name length + null byte */
  79. sqlite3_mutex *p;
  80. switch( iType ){
  81. case SQLITE_MUTEX_FAST:
  82. case SQLITE_MUTEX_RECURSIVE: {
  83. p = sqlite3MallocZero( sizeof(*p) );
  84. if( p ){
  85. p->mutexName = (PSZ)malloc(mutex_name_len);
  86. sqlite3_snprintf(mutex_name_len, p->mutexName, "%s", mutex_name);
  87. p->id = iType;
  88. DosCreateMutexSem(p->mutexName, &p->mutex, 0, FALSE);
  89. DosOpenMutexSem(p->mutexName, &p->mutex);
  90. }
  91. break;
  92. }
  93. default: {
  94. static sqlite3_mutex staticMutexes[5];
  95. static int isInit = 0;
  96. while( !isInit ) {
  97. static long lock = 0;
  98. DosEnterCritSec();
  99. lock++;
  100. if( lock == 1 ) {
  101. DosExitCritSec();
  102. int i;
  103. for(i = 0; i < sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++) {
  104. staticMutexes[i].mutexName = (PSZ)malloc(mutex_name_len + 1);
  105. sqlite3_snprintf(mutex_name_len + 1, /* one more for the number */
  106. staticMutexes[i].mutexName, "%s%1d", mutex_name, i);
  107. DosCreateMutexSem(staticMutexes[i].mutexName,
  108. &staticMutexes[i].mutex, 0, FALSE);
  109. DosOpenMutexSem(staticMutexes[i].mutexName,
  110. &staticMutexes[i].mutex);
  111. }
  112. isInit = 1;
  113. } else {
  114. DosExitCritSec();
  115. DosSleep(1);
  116. }
  117. }
  118. assert( iType-2 >= 0 );
  119. assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
  120. p = &staticMutexes[iType-2];
  121. p->id = iType;
  122. break;
  123. }
  124. }
  125. return p;
  126. }
  127. /*
  128. ** This routine deallocates a previously allocated mutex.
  129. ** SQLite is careful to deallocate every mutex that it allocates.
  130. */
  131. void sqlite3_mutex_free(sqlite3_mutex *p){
  132. assert( p );
  133. assert( p->nRef==0 );
  134. assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
  135. DosCloseMutexSem(p->mutex);
  136. free(p->mutexName);
  137. sqlite3_free(p);
  138. }
  139. /*
  140. ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
  141. ** to enter a mutex. If another thread is already within the mutex,
  142. ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
  143. ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
  144. ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
  145. ** be entered multiple times by the same thread. In such cases the,
  146. ** mutex must be exited an equal number of times before another thread
  147. ** can enter. If the same thread tries to enter any other kind of mutex
  148. ** more than once, the behavior is undefined.
  149. */
  150. void sqlite3_mutex_enter(sqlite3_mutex *p){
  151. TID tid;
  152. PID holder1;
  153. ULONG holder2;
  154. assert( p );
  155. assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
  156. DosRequestMutexSem(p->mutex, SEM_INDEFINITE_WAIT);
  157. DosQueryMutexSem(p->mutex, &holder1, &tid, &holder2);
  158. p->owner = tid;
  159. p->nRef++;
  160. }
  161. int sqlite3_mutex_try(sqlite3_mutex *p){
  162. int rc;
  163. TID tid;
  164. PID holder1;
  165. ULONG holder2;
  166. assert( p );
  167. assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
  168. if( DosRequestMutexSem(p->mutex, SEM_IMMEDIATE_RETURN) == NO_ERROR) {
  169. DosQueryMutexSem(p->mutex, &holder1, &tid, &holder2);
  170. p->owner = tid;
  171. p->nRef++;
  172. rc = SQLITE_OK;
  173. } else {
  174. rc = SQLITE_BUSY;
  175. }
  176. return rc;
  177. }
  178. /*
  179. ** The sqlite3_mutex_leave() routine exits a mutex that was
  180. ** previously entered by the same thread. The behavior
  181. ** is undefined if the mutex is not currently entered or
  182. ** is not currently allocated. SQLite will never do either.
  183. */
  184. void sqlite3_mutex_leave(sqlite3_mutex *p){
  185. TID tid;
  186. PID holder1;
  187. ULONG holder2;
  188. assert( p->nRef>0 );
  189. DosQueryMutexSem(p->mutex, &holder1, &tid, &holder2);
  190. assert( p->owner==tid );
  191. p->nRef--;
  192. assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
  193. DosReleaseMutexSem(p->mutex);
  194. }
  195. /*
  196. ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
  197. ** intended for use inside assert() statements.
  198. */
  199. int sqlite3_mutex_held(sqlite3_mutex *p){
  200. TID tid;
  201. PID pid;
  202. ULONG ulCount;
  203. PTIB ptib;
  204. if( p!=0 ) {
  205. DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
  206. } else {
  207. DosGetInfoBlocks(&ptib, NULL);
  208. tid = ptib->tib_ptib2->tib2_ultid;
  209. }
  210. return p==0 || (p->nRef!=0 && p->owner==tid);
  211. }
  212. int sqlite3_mutex_notheld(sqlite3_mutex *p){
  213. TID tid;
  214. PID pid;
  215. ULONG ulCount;
  216. PTIB ptib;
  217. if( p!= 0 ) {
  218. DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
  219. } else {
  220. DosGetInfoBlocks(&ptib, NULL);
  221. tid = ptib->tib_ptib2->tib2_ultid;
  222. }
  223. return p==0 || p->nRef==0 || p->owner!=tid;
  224. }
  225. #endif /* SQLITE_MUTEX_OS2 */