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

  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include </usr/include/thread.h>
  7. #undef _POSIX_THREADS
  8.  
  9.  
  10. /*
  11.  * Initialization.
  12.  */
  13. static void PyThread__init_thread(void)
  14. {
  15. }
  16.  
  17. /*
  18.  * Thread support.
  19.  */
  20. struct func_arg {
  21.     void (*func)(void *);
  22.     void *arg;
  23. };
  24.  
  25. static void *
  26. new_func(void *funcarg)
  27. {
  28.     void (*func)(void *);
  29.     void *arg;
  30.  
  31.     func = ((struct func_arg *) funcarg)->func;
  32.     arg = ((struct func_arg *) funcarg)->arg;
  33.     free(funcarg);
  34.     (*func)(arg);
  35.     return 0;
  36. }
  37.  
  38.  
  39. int 
  40. PyThread_start_new_thread(void (*func)(void *), void *arg)
  41. {
  42.     struct func_arg *funcarg;
  43.     int success = 0;    /* init not needed when SOLARIS_THREADS and */
  44.                 /* C_THREADS implemented properly */
  45.  
  46.     dprintf(("PyThread_start_new_thread called\n"));
  47.     if (!initialized)
  48.         PyThread_init_thread();
  49.     funcarg = (struct func_arg *) malloc(sizeof(struct func_arg));
  50.     funcarg->func = func;
  51.     funcarg->arg = arg;
  52.     if (thr_create(0, 0, new_func, funcarg,
  53.                THR_DETACHED | THR_NEW_LWP, 0)) {
  54.         perror("thr_create");
  55.         free((void *) funcarg);
  56.         success = -1;
  57.     }
  58.     return success < 0 ? 0 : 1;
  59. }
  60.  
  61. long
  62. PyThread_get_thread_ident(void)
  63. {
  64.     if (!initialized)
  65.         PyThread_init_thread();
  66.     return thr_self();
  67. }
  68.  
  69. static void 
  70. do_PyThread_exit_thread(int no_cleanup)
  71. {
  72.     dprintf(("PyThread_exit_thread called\n"));
  73.     if (!initialized)
  74.         if (no_cleanup)
  75.             _exit(0);
  76.         else
  77.             exit(0);
  78.     thr_exit(0);
  79. }
  80.  
  81. void 
  82. PyThread_exit_thread(void)
  83. {
  84.     do_PyThread_exit_thread(0);
  85. }
  86.  
  87. void 
  88. PyThread__exit_thread(void)
  89. {
  90.     do_PyThread_exit_thread(1);
  91. }
  92.  
  93. #ifndef NO_EXIT_PROG
  94. static void 
  95. do_PyThread_exit_prog(int status, int no_cleanup)
  96. {
  97.     dprintf(("PyThread_exit_prog(%d) called\n", status));
  98.     if (!initialized)
  99.         if (no_cleanup)
  100.             _exit(status);
  101.         else
  102.             exit(status);
  103.     if (no_cleanup)
  104.         _exit(status);
  105.     else
  106.         exit(status);
  107. }
  108.  
  109. void 
  110. PyThread_exit_prog(int status)
  111. {
  112.     do_PyThread_exit_prog(status, 0);
  113. }
  114.  
  115. void 
  116. PyThread__exit_prog(int status)
  117. {
  118.     do_PyThread_exit_prog(status, 1);
  119. }
  120. #endif /* NO_EXIT_PROG */
  121.  
  122. /*
  123.  * Lock support.
  124.  */
  125. PyThread_type_lock 
  126. PyThread_allocate_lock(void)
  127. {
  128.     mutex_t *lock;
  129.  
  130.     dprintf(("PyThread_allocate_lock called\n"));
  131.     if (!initialized)
  132.         PyThread_init_thread();
  133.  
  134.     lock = (mutex_t *) malloc(sizeof(mutex_t));
  135.     if (mutex_init(lock, USYNC_THREAD, 0)) {
  136.         perror("mutex_init");
  137.         free((void *) lock);
  138.         lock = 0;
  139.     }
  140.     dprintf(("PyThread_allocate_lock() -> %p\n", lock));
  141.     return (PyThread_type_lock) lock;
  142. }
  143.  
  144. void 
  145. PyThread_free_lock(PyThread_type_lock lock)
  146. {
  147.     dprintf(("PyThread_free_lock(%p) called\n", lock));
  148.     mutex_destroy((mutex_t *) lock);
  149.     free((void *) lock);
  150. }
  151.  
  152. int 
  153. PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
  154. {
  155.     int success;
  156.  
  157.     dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
  158.     if (waitflag)
  159.         success = mutex_lock((mutex_t *) lock);
  160.     else
  161.         success = mutex_trylock((mutex_t *) lock);
  162.     if (success < 0)
  163.         perror(waitflag ? "mutex_lock" : "mutex_trylock");
  164.     else
  165.         success = !success; /* solaris does it the other way round */
  166.     dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
  167.     return success;
  168. }
  169.  
  170. void 
  171. PyThread_release_lock(PyThread_type_lock lock)
  172. {
  173.     dprintf(("PyThread_release_lock(%p) called\n", lock));
  174.     if (mutex_unlock((mutex_t *) lock))
  175.         perror("mutex_unlock");
  176. }
  177.  
  178. /*
  179.  * Semaphore support.
  180.  */
  181. PyThread_type_sema 
  182. PyThread_allocate_sema(int value)
  183. {
  184.     sema_t *sema;
  185.     dprintf(("PyThread_allocate_sema called\n"));
  186.     if (!initialized)
  187.         PyThread_init_thread();
  188.  
  189.     sema = (sema_t *) malloc(sizeof(sema_t));
  190.     if (sema_init(sema, value, USYNC_THREAD, 0)) {
  191.         perror("sema_init");
  192.         free((void *) sema);
  193.         sema = 0;
  194.     }
  195.     dprintf(("PyThread_allocate_sema() -> %p\n",  sema));
  196.     return (PyThread_type_sema) sema;
  197. }
  198.  
  199. void 
  200. PyThread_free_sema(PyThread_type_sema sema)
  201. {
  202.     dprintf(("PyThread_free_sema(%p) called\n",  sema));
  203.     if (sema_destroy((sema_t *) sema))
  204.         perror("sema_destroy");
  205.     free((void *) sema);
  206. }
  207.  
  208. int 
  209. PyThread_down_sema(PyThread_type_sema sema, int waitflag)
  210. {
  211.     int success;
  212.  
  213.     dprintf(("PyThread_down_sema(%p) called\n",  sema));
  214.     if (waitflag)
  215.         success = sema_wait((sema_t *) sema);
  216.     else
  217.         success = sema_trywait((sema_t *) sema);
  218.     if (success < 0) {
  219.         if (errno == EBUSY)
  220.             success = 0;
  221.         else
  222.             perror("sema_wait");
  223.     }
  224.     else
  225.         success = !success;
  226.     dprintf(("PyThread_down_sema(%p) return %d\n",  sema, success));
  227.     return success;
  228. }
  229.  
  230. void 
  231. PyThread_up_sema(PyThread_type_sema sema)
  232. {
  233.     dprintf(("PyThread_up_sema(%p)\n",  sema));
  234.     if (sema_post((sema_t *) sema))
  235.         perror("sema_post");
  236. }
  237.