home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Python / thread_pthread.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-28  |  7.6 KB  |  295 lines  |  [TEXT/CWIE]

  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. /* Posix threads interface */
  33.  
  34. #include <stdlib.h>
  35. #include <pthread.h>
  36.  
  37. #ifdef _AIX
  38.  
  39. #ifndef SCHED_BG_NP
  40. /* SCHED_BG_NP is defined if using AIX DCE pthreads
  41.  * but it is unsupported by AIX 4 pthreads. Default
  42.  * attributes for AIX 4 pthreads equal to NULL. For
  43.  * AIX DCE pthreads they should be left unchanged.
  44.  */
  45. #define pthread_attr_default NULL
  46. #define pthread_mutexattr_default NULL
  47. #define pthread_condattr_default NULL
  48. #endif
  49.  
  50. #else
  51. #define pthread_attr_default ((pthread_attr_t *)0)
  52. #define pthread_mutexattr_default ((pthread_mutexattr_t *)0)
  53. #define pthread_condattr_default ((pthread_condattr_t *)0)
  54. #endif
  55.  
  56. /* A pthread mutex isn't sufficient to model the Python lock type
  57.  * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
  58.  * following are undefined:
  59.  *  -> a thread tries to lock a mutex it already has locked
  60.  *  -> a thread tries to unlock a mutex locked by a different thread
  61.  * pthread mutexes are designed for serializing threads over short pieces
  62.  * of code anyway, so wouldn't be an appropriate implementation of
  63.  * Python's locks regardless.
  64.  *
  65.  * The pthread_lock struct implements a Python lock as a "locked?" bit
  66.  * and a <condition, mutex> pair.  In general, if the bit can be acquired
  67.  * instantly, it is, else the pair is used to block the thread until the
  68.  * bit is cleared.     9 May 1994 tim@ksr.com
  69.  */
  70.  
  71. typedef struct {
  72.     char             locked; /* 0=unlocked, 1=locked */
  73.     /* a <cond, mutex> pair to handle an acquire of a locked lock */
  74.     pthread_cond_t   lock_released;
  75.     pthread_mutex_t  mut;
  76. } pthread_lock;
  77.  
  78. #define CHECK_STATUS(name)  if (status < 0) { perror(name); error=1; }
  79.  
  80. /*
  81.  * Initialization.
  82.  */
  83. static void _init_thread _P0()
  84. {
  85. }
  86.  
  87. /*
  88.  * Thread support.
  89.  */
  90.  
  91.  
  92. int start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
  93. {
  94.     pthread_t th;
  95.     int success;
  96.     dprintf(("start_new_thread called\n"));
  97.     if (!initialized)
  98.         init_thread();
  99.     success = pthread_create(&th, pthread_attr_default,
  100.                  (void* (*) _P((void *)))func, arg);
  101.     return success < 0 ? 0 : 1;
  102. }
  103.  
  104. long get_thread_ident _P0()
  105. {
  106.     pthread_t threadid;
  107.     if (!initialized)
  108.         init_thread();
  109.     /* Jump through some hoops for Alpha OSF/1 */
  110.     threadid = pthread_self();
  111.     return (long) *(long *) &threadid;
  112. }
  113.  
  114. static void do_exit_thread _P1(no_cleanup, int no_cleanup)
  115. {
  116.     dprintf(("exit_thread called\n"));
  117.     if (!initialized)
  118.         if (no_cleanup)
  119.             _exit(0);
  120.         else
  121.             exit(0);
  122. }
  123.  
  124. void exit_thread _P0()
  125. {
  126.     do_exit_thread(0);
  127. }
  128.  
  129. void _exit_thread _P0()
  130. {
  131.     do_exit_thread(1);
  132. }
  133.  
  134. #ifndef NO_EXIT_PROG
  135. static void do_exit_prog _P2(status, int status, no_cleanup, int no_cleanup)
  136. {
  137.     dprintf(("exit_prog(%d) called\n", status));
  138.     if (!initialized)
  139.         if (no_cleanup)
  140.             _exit(status);
  141.         else
  142.             exit(status);
  143. }
  144.  
  145. void exit_prog _P1(status, int status)
  146. {
  147.     do_exit_prog(status, 0);
  148. }
  149.  
  150. void _exit_prog _P1(status, int status)
  151. {
  152.     do_exit_prog(status, 1);
  153. }
  154. #endif /* NO_EXIT_PROG */
  155.  
  156. /*
  157.  * Lock support.
  158.  */
  159. type_lock allocate_lock _P0()
  160. {
  161.     pthread_lock *lock;
  162.     int status, error = 0;
  163.  
  164.     dprintf(("allocate_lock called\n"));
  165.     if (!initialized)
  166.         init_thread();
  167.  
  168.     lock = (pthread_lock *) malloc(sizeof(pthread_lock));
  169.     if (lock) {
  170.         lock->locked = 0;
  171.  
  172.         status = pthread_mutex_init(&lock->mut,
  173.                         pthread_mutexattr_default);
  174.         CHECK_STATUS("pthread_mutex_init");
  175.  
  176.         status = pthread_cond_init(&lock->lock_released,
  177.                        pthread_condattr_default);
  178.         CHECK_STATUS("pthread_cond_init");
  179.  
  180.         if (error) {
  181.             free((void *)lock);
  182.             lock = 0;
  183.         }
  184.     }
  185.  
  186.     dprintf(("allocate_lock() -> %lx\n", (long)lock));
  187.     return (type_lock) lock;
  188. }
  189.  
  190. void free_lock _P1(lock, type_lock lock)
  191. {
  192.     pthread_lock *thelock = (pthread_lock *)lock;
  193.     int status, error = 0;
  194.  
  195.     dprintf(("free_lock(%lx) called\n", (long)lock));
  196.  
  197.     status = pthread_mutex_destroy( &thelock->mut );
  198.     CHECK_STATUS("pthread_mutex_destroy");
  199.  
  200.     status = pthread_cond_destroy( &thelock->lock_released );
  201.     CHECK_STATUS("pthread_cond_destroy");
  202.  
  203.     free((void *)thelock);
  204. }
  205.  
  206. int acquire_lock _P2(lock, type_lock lock, waitflag, int waitflag)
  207. {
  208.     int success;
  209.     pthread_lock *thelock = (pthread_lock *)lock;
  210.     int status, error = 0;
  211.  
  212.     dprintf(("acquire_lock(%lx, %d) called\n", (long)lock, waitflag));
  213.  
  214.     status = pthread_mutex_lock( &thelock->mut );
  215.     CHECK_STATUS("pthread_mutex_lock[1]");
  216.     success = thelock->locked == 0;
  217.     if (success) thelock->locked = 1;
  218.     status = pthread_mutex_unlock( &thelock->mut );
  219.     CHECK_STATUS("pthread_mutex_unlock[1]");
  220.  
  221.     if ( !success && waitflag ) {
  222.         /* continue trying until we get the lock */
  223.  
  224.         /* mut must be locked by me -- part of the condition
  225.          * protocol */
  226.         status = pthread_mutex_lock( &thelock->mut );
  227.         CHECK_STATUS("pthread_mutex_lock[2]");
  228.         while ( thelock->locked ) {
  229.             status = pthread_cond_wait(&thelock->lock_released,
  230.                            &thelock->mut);
  231.             CHECK_STATUS("pthread_cond_wait");
  232.         }
  233.         thelock->locked = 1;
  234.         status = pthread_mutex_unlock( &thelock->mut );
  235.         CHECK_STATUS("pthread_mutex_unlock[2]");
  236.         success = 1;
  237.     }
  238.     if (error) success = 0;
  239.     dprintf(("acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success));
  240.     return success;
  241. }
  242.  
  243. void release_lock _P1(lock, type_lock lock)
  244. {
  245.     pthread_lock *thelock = (pthread_lock *)lock;
  246.     int status, error = 0;
  247.  
  248.     dprintf(("release_lock(%lx) called\n", (long)lock));
  249.  
  250.     status = pthread_mutex_lock( &thelock->mut );
  251.     CHECK_STATUS("pthread_mutex_lock[3]");
  252.  
  253.     thelock->locked = 0;
  254.  
  255.     status = pthread_mutex_unlock( &thelock->mut );
  256.     CHECK_STATUS("pthread_mutex_unlock[3]");
  257.  
  258.     /* wake up someone (anyone, if any) waiting on the lock */
  259.     status = pthread_cond_signal( &thelock->lock_released );
  260.     CHECK_STATUS("pthread_cond_signal");
  261. }
  262.  
  263. /*
  264.  * Semaphore support.
  265.  */
  266. /* NOTE: 100% non-functional at this time - tim */
  267.  
  268. type_sema allocate_sema _P1(value, int value)
  269. {
  270.     char *sema = 0;
  271.     dprintf(("allocate_sema called\n"));
  272.     if (!initialized)
  273.         init_thread();
  274.  
  275.     dprintf(("allocate_sema() -> %lx\n", (long) sema));
  276.     return (type_sema) sema;
  277. }
  278.  
  279. void free_sema _P1(sema, type_sema sema)
  280. {
  281.     dprintf(("free_sema(%lx) called\n", (long) sema));
  282. }
  283.  
  284. int down_sema _P2(sema, type_sema sema, waitflag, int waitflag)
  285. {
  286.     dprintf(("down_sema(%lx, %d) called\n", (long) sema, waitflag));
  287.     dprintf(("down_sema(%lx) return\n", (long) sema));
  288.     return -1;
  289. }
  290.  
  291. void up_sema _P1(sema, type_sema sema)
  292. {
  293.     dprintf(("up_sema(%lx)\n", (long) sema));
  294. }
  295.