home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / py2s152.zip / Python / thread_os2.h < prev    next >
C/C++ Source or Header  |  1999-06-27  |  6KB  |  251 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.   
  30. ******************************************************************/
  31.  
  32. /* This code implemented by cvale@netcom.com */
  33.  
  34. #define INCL_DOSPROCESS
  35. #define INCL_DOSSEMAPHORES
  36. #include "os2.h"
  37. #include "limits.h"
  38.  
  39. #include "process.h"
  40.  
  41. long PyThread_get_thread_ident(void);
  42.  
  43.  
  44. /*
  45.  * Initialization of the C package, should not be needed.
  46.  */
  47. static void PyThread__init_thread(void)
  48. {
  49. }
  50.  
  51. /*
  52.  * Thread support.
  53.  */
  54. int PyThread_start_new_thread(void (*func)(void *), void *arg)
  55. {
  56.   int aThread;
  57.   int success = 1;
  58.  
  59.   aThread = _beginthread(func,NULL,360448,arg);
  60.  
  61.   if( aThread == -1 ) {
  62.     success = 0;
  63.     fprintf(stderr,"aThread failed == %d",aThread);
  64.     dprintf(("_beginthread failed. return %ld\n", errno));
  65.   }
  66.  
  67.   return success;
  68. }
  69.  
  70. long PyThread_get_thread_ident(void)
  71. {
  72.   PPIB pib;
  73.   PTIB tib;
  74.  
  75.   if (!initialized)
  76.     PyThread_init_thread();
  77.         
  78.   DosGetInfoBlocks(&tib,&pib);
  79.   return tib->tib_ptib2->tib2_ultid;
  80. }
  81.  
  82. static void do_PyThread_exit_thread(int no_cleanup)
  83. {
  84.   dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
  85.   if (!initialized)
  86.     if (no_cleanup)
  87.       _exit(0);
  88.     else
  89.       exit(0);
  90.   _endthread();
  91. }
  92.  
  93. void PyThread_exit_thread(void)
  94. {
  95.   do_PyThread_exit_thread(0);
  96. }
  97.  
  98. void PyThread__exit_thread(void)
  99. {
  100.   do_PyThread_exit_thread(1);
  101. }
  102.  
  103. #ifndef NO_EXIT_PROG
  104. static void do_PyThread_exit_prog(int status, int no_cleanup)
  105. {
  106.   dprintf(("PyThread_exit_prog(%d) called\n", status));
  107.   if (!initialized)
  108.     if (no_cleanup)
  109.       _exit(status);
  110.     else
  111.       exit(status);
  112. }
  113.  
  114. void PyThread_exit_prog(int status)
  115. {
  116.   do_PyThread_exit_prog(status, 0);
  117. }
  118.  
  119. void PyThread__exit_prog _P1(int status)
  120. {
  121.   do_PyThread_exit_prog(status, 1);
  122. }
  123. #endif /* NO_EXIT_PROG */
  124.  
  125. typedef struct os2_lock_t {
  126.   int is_set;
  127.   HEV changed;
  128. } *type_os2_lock;
  129.  
  130. /*
  131.  * Lock support. It has too be implemented as semaphores.
  132.  * Reimplemented this using an event semaphore and critical sections
  133.  * to behave more like a posix mutex semaphore.
  134.  */
  135. PyThread_type_lock PyThread_allocate_lock(void)
  136. {
  137.   APIRET rc;
  138.   type_os2_lock lock = (type_os2_lock)malloc(sizeof(struct os2_lock_t));
  139.  
  140.   dprintf(("PyThread_allocate_lock called\n"));
  141.   if (!initialized)
  142.     PyThread_init_thread();
  143.  
  144.   lock->is_set = 0;
  145.  
  146.   DosCreateEventSem(NULL, &lock->changed, 0, 0);
  147.  
  148.   dprintf(("%ld: PyThread_allocate_lock() -> %lx\n", 
  149.            PyThread_get_thread_ident(), 
  150.            (long)lock->changed));
  151.  
  152.   return (PyThread_type_lock) lock;
  153. }
  154.  
  155. void PyThread_free_lock(PyThread_type_lock aLock)
  156. {
  157.   type_os2_lock lock = (type_os2_lock)aLock;
  158.   dprintf(("%ld: PyThread_free_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock));
  159.  
  160.   DosCloseEventSem(lock->changed);
  161.   free(aLock);
  162. }
  163.  
  164. /*
  165.  * Return 1 on success if the lock was acquired
  166.  * and 0 if the lock was not acquired.
  167.  */
  168. int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
  169. {
  170.   int   done = 0;
  171.   ULONG count;
  172.   PID   pid = 0;
  173.   TID   tid = 0;
  174.   type_os2_lock lock = (type_os2_lock)aLock;
  175.  
  176.   dprintf(("%ld: PyThread_acquire_lock(%lx, %d) called\n", PyThread_get_thread_ident(),
  177.            (long)aLock, waitflag));
  178.  
  179.   while (!done) {
  180.     /* if the lock is currently set, we have to wait for the state to change */
  181.     if (lock->is_set) {
  182.       if (!waitflag)
  183.         return 0;
  184.       DosWaitEventSem(lock->changed, SEM_INDEFINITE_WAIT);
  185.     }
  186.  
  187.     /* 
  188.      * enter a critical section and try to get the semaphore.  If
  189.      * it is still locked, we will try again.
  190.      */
  191.     if (DosEnterCritSec())
  192.       return 0;
  193.  
  194.     if (!lock->is_set) {
  195.       lock->is_set = 1;
  196.       DosResetEventSem(lock->changed, &count);
  197.       done = 1;
  198.     }
  199.  
  200.     DosExitCritSec();
  201.   }
  202.  
  203.   return 1;
  204. }
  205.  
  206. void PyThread_release_lock(PyThread_type_lock aLock)
  207. {
  208.   type_os2_lock lock = (type_os2_lock)aLock;
  209.   dprintf(("%ld: PyThread_release_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock));
  210.  
  211.   if (!lock->is_set) {
  212.     dprintf(("%ld: Could not PyThread_release_lock(%lx) error: %l\n",
  213.              PyThread_get_thread_ident(), (long)aLock, GetLastError()));
  214.     return;
  215.   }
  216.  
  217.   if (DosEnterCritSec()) {
  218.     dprintf(("%ld: Could not PyThread_release_lock(%lx) error: %l\n",
  219.              PyThread_get_thread_ident(), (long)aLock, GetLastError()));
  220.     return;
  221.   }
  222.  
  223.   lock->is_set = 0;
  224.   DosPostEventSem(lock->changed);
  225.  
  226.   DosExitCritSec();
  227. }
  228.  
  229. /*
  230.  * Semaphore support.
  231.  */
  232. PyThread_type_sema PyThread_allocate_sema(int value)
  233. {
  234.   return (PyThread_type_sema) 0;
  235. }
  236.  
  237. void PyThread_free_sema(PyThread_type_sema aSemaphore)
  238. {
  239.  
  240. }
  241.  
  242. int PyThread_down_sema(PyThread_type_sema aSemaphore, int waitflag)
  243. {
  244.   return -1;
  245. }
  246.  
  247. void PyThread_up_sema(PyThread_type_sema aSemaphore)
  248. {
  249.   dprintf(("%ld: PyThread_up_sema(%lx)\n", PyThread_get_thread_ident(), (long)aSemaphore));
  250. }
  251.