pthread.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * A simple clopne based pthread implementation
  4. *
  5. * Copyright (C) 2001 by Erik Andersen <andersee@debian.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <stdlib.h>
  22. #include <sched.h>
  23. #include <signal.h>
  24. #include <errno.h>
  25. #include <unistd.h>
  26. #include <pthread.h>
  27. #define STACKSIZE 8096
  28. #define CSIGNAL 0x000000ff /* signal mask to be sent at exit */
  29. #define CLONE_VM 0x00000100 /* set if VM shared between processes */
  30. #define CLONE_FS 0x00000200 /* set if fs info shared between proces ses */
  31. #define CLONE_FILES 0x00000400 /* set if open files shared between pro cesses */
  32. #define CLONE_SIGHAND 0x00000800 /* set if signal handlers shared */
  33. /* Lame home-grown clone based threading */
  34. int pthread_mutex_init (pthread_mutex_t *mutex, const pthread_mutexattr_t *mutex_attr)
  35. {
  36. mutex->__m_lock.__spinlock = 1;
  37. return 0;
  38. }
  39. int pthread_mutex_lock (pthread_mutex_t *mutex)
  40. {
  41. while (mutex->__m_lock.__spinlock == 0) {
  42. usleep(10000);
  43. }
  44. --(mutex->__m_lock.__spinlock);
  45. return 0;
  46. }
  47. int pthread_mutex_unlock (pthread_mutex_t *mutex)
  48. {
  49. ++(mutex->__m_lock.__spinlock);
  50. return 0;
  51. }
  52. int pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex)
  53. {
  54. ++(mutex->__m_lock.__spinlock);
  55. while (cond->__c_lock.__spinlock == 0) {
  56. usleep(10000);
  57. }
  58. --(cond->__c_lock.__spinlock);
  59. return 0;
  60. }
  61. int pthread_cond_signal(pthread_cond_t *cond)
  62. {
  63. ++(cond->__c_lock.__spinlock);
  64. return 0;
  65. }
  66. int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
  67. {
  68. cond->__c_lock.__spinlock = 1;
  69. return 0;
  70. }
  71. int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void* (*fn)(void *), void *data)
  72. {
  73. long retval;
  74. void **newstack;
  75. int (*clonefunc)(void *) = (int (*)(void *))(fn);
  76. newstack = (void **) malloc(STACKSIZE);
  77. if (!newstack)
  78. return -1;
  79. newstack = (void **) (STACKSIZE + (char *) newstack);
  80. *--newstack = data;
  81. retval = clone(clonefunc, newstack,
  82. CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD, data);
  83. if (retval < 0) {
  84. errno = -retval;
  85. *thread = 0;
  86. retval = -1;
  87. } else {
  88. *thread = retval;
  89. retval = 0;
  90. }
  91. return retval;
  92. }