lock_pthread.c 677 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /* include my_lock_init */
  2. #include "unpthread.h"
  3. #include <sys/mman.h>
  4. static pthread_mutex_t *mptr; /* actual mutex will be in shared memory */
  5. void
  6. my_lock_init(char *pathname)
  7. {
  8. int fd;
  9. pthread_mutexattr_t mattr;
  10. fd = Open("/dev/zero", O_RDWR, 0);
  11. mptr = Mmap(0, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE,
  12. MAP_SHARED, fd, 0);
  13. Close(fd);
  14. Pthread_mutexattr_init(&mattr);
  15. Pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
  16. Pthread_mutex_init(mptr, &mattr);
  17. }
  18. /* end my_lock_init */
  19. /* include my_lock_wait */
  20. void
  21. my_lock_wait()
  22. {
  23. Pthread_mutex_lock(mptr);
  24. }
  25. void
  26. my_lock_release()
  27. {
  28. Pthread_mutex_unlock(mptr);
  29. }
  30. /* end my_lock_wait */