home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Python / thread_os2.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-25  |  4.1 KB  |  208 lines

  1. /* This code implemented by cvale@netcom.com */
  2.  
  3. #define INCL_DOSPROCESS
  4. #define INCL_DOSSEMAPHORES
  5. #include "os2.h"
  6. #include "limits.h"
  7.  
  8. #include "process.h"
  9.  
  10. long PyThread_get_thread_ident(void);
  11.  
  12.  
  13. /*
  14.  * Initialization of the C package, should not be needed.
  15.  */
  16. static void
  17. PyThread__init_thread(void)
  18. {
  19. }
  20.  
  21. /*
  22.  * Thread support.
  23.  */
  24. int
  25. PyThread_start_new_thread(void (*func)(void *), void *arg)
  26. {
  27.   int aThread;
  28.   int success = 1;
  29.  
  30.   aThread = _beginthread(func,NULL,65536,arg);
  31.  
  32.   if( aThread == -1 ) {
  33.     success = 0;
  34.     fprintf(stderr,"aThread failed == %d",aThread);
  35.     dprintf(("_beginthread failed. return %ld\n", errno));
  36.   }
  37.  
  38.   return success;
  39. }
  40.  
  41. long
  42. PyThread_get_thread_ident(void)
  43. {
  44.   PPIB pib;
  45.   PTIB tib;
  46.  
  47.   if (!initialized)
  48.     PyThread_init_thread();
  49.         
  50.   DosGetInfoBlocks(&tib,&pib);
  51.   return tib->tib_ptib2->tib2_ultid;
  52. }
  53.  
  54. static void
  55. do_PyThread_exit_thread(int no_cleanup)
  56. {
  57.   dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
  58.   if (!initialized)
  59.     if (no_cleanup)
  60.       _exit(0);
  61.     else
  62.       exit(0);
  63.   _endthread();
  64. }
  65.  
  66. void 
  67. PyThread_exit_thread(void)
  68. {
  69.   do_PyThread_exit_thread(0);
  70. }
  71.  
  72. void 
  73. PyThread__exit_thread(void)
  74. {
  75.   do_PyThread_exit_thread(1);
  76. }
  77.  
  78. #ifndef NO_EXIT_PROG
  79. static void 
  80. do_PyThread_exit_prog(int status, int no_cleanup)
  81. {
  82.   dprintf(("PyThread_exit_prog(%d) called\n", status));
  83.   if (!initialized)
  84.     if (no_cleanup)
  85.       _exit(status);
  86.     else
  87.       exit(status);
  88. }
  89.  
  90. void 
  91. PyThread_exit_prog(int status)
  92. {
  93.   do_PyThread_exit_prog(status, 0);
  94. }
  95.  
  96. void 
  97. PyThread__exit_prog(int status)
  98. {
  99.   do_PyThread_exit_prog(status, 1);
  100. }
  101. #endif /* NO_EXIT_PROG */
  102.  
  103. /*
  104.  * Lock support. It has too be implemented as semaphores.
  105.  * I [Dag] tried to implement it with mutex but I could find a way to
  106.  * tell whether a thread already own the lock or not.
  107.  */
  108. PyThread_type_lock 
  109. PyThread_allocate_lock(void)
  110. {
  111.   HMTX   aLock;
  112.   APIRET rc;
  113.  
  114.   dprintf(("PyThread_allocate_lock called\n"));
  115.   if (!initialized)
  116.     PyThread_init_thread();
  117.  
  118.   DosCreateMutexSem(NULL,  /* Sem name      */
  119.                     &aLock, /* the semaphore */
  120.                     0,     /* shared ?      */
  121.                     0);    /* initial state */  
  122.  
  123.   dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));
  124.  
  125.   return (PyThread_type_lock) aLock;
  126. }
  127.  
  128. void 
  129. PyThread_free_lock(PyThread_type_lock aLock)
  130. {
  131.   dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
  132.  
  133.   DosCloseMutexSem((HMTX)aLock);
  134. }
  135.  
  136. /*
  137.  * Return 1 on success if the lock was acquired
  138.  *
  139.  * and 0 if the lock was not acquired. This means a 0 is returned
  140.  * if the lock has already been acquired by this thread!
  141.  */
  142. int 
  143. PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
  144. {
  145.   int   success = 1;
  146.   ULONG rc, count;
  147.   PID   pid = 0;
  148.   TID   tid = 0;
  149.  
  150.   dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),
  151.            aLock, waitflag));
  152.  
  153.   DosQueryMutexSem((HMTX)aLock,&pid,&tid,&count);
  154.   if( tid == PyThread_get_thread_ident() ) { /* if we own this lock */
  155.     success = 0;
  156.   } else {
  157.     rc = DosRequestMutexSem((HMTX) aLock,
  158.                             (waitflag == 1 ? SEM_INDEFINITE_WAIT : 0));
  159.     
  160.     if( rc != 0) {
  161.       success = 0;                /* We failed */
  162.     }
  163.   }
  164.  
  165.   dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n",
  166.            PyThread_get_thread_ident(),aLock, waitflag, success));
  167.  
  168.   return success;
  169. }
  170.  
  171. void 
  172. PyThread_release_lock(PyThread_type_lock aLock)
  173. {
  174.   dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
  175.  
  176.   if ( DosReleaseMutexSem( (HMTX) aLock ) != 0 ) {
  177.     dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
  178.              PyThread_get_thread_ident(), aLock, GetLastError()));
  179.   }
  180. }
  181.  
  182. /*
  183.  * Semaphore support.
  184.  */
  185. PyThread_type_sema 
  186. PyThread_allocate_sema(int value)
  187. {
  188.   return (PyThread_type_sema) 0;
  189. }
  190.  
  191. void 
  192. PyThread_free_sema(PyThread_type_sema aSemaphore)
  193. {
  194.  
  195. }
  196.  
  197. int 
  198. PyThread_down_sema(PyThread_type_sema aSemaphore, int waitflag)
  199. {
  200.   return -1;
  201. }
  202.  
  203. void 
  204. PyThread_up_sema(PyThread_type_sema aSemaphore)
  205. {
  206.   dprintf(("%ld: PyThread_up_sema(%p)\n", PyThread_get_thread_ident(), aSemaphore));
  207. }
  208.