📄 pthread.c
字号:
if (!pth_atfork_push(pthread_atfork_cb_prepare, pthread_atfork_cb_parent, pthread_atfork_cb_child, info)) return errno; return OK;}/*** MUTEX ATTRIBUTE ROUTINES*/int pthread_mutexattr_init(pthread_mutexattr_t *attr){ pthread_initialize(); if (attr == NULL) return pth_error(EINVAL, EINVAL); /* nothing to do for us */ return OK;}int pthread_mutexattr_destroy(pthread_mutexattr_t *attr){ if (attr == NULL) return pth_error(EINVAL, EINVAL); /* nothing to do for us */ return OK;}int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling){ if (attr == NULL) return pth_error(EINVAL, EINVAL); /* not supported */ return pth_error(ENOSYS, ENOSYS);}int pthread_mutexattr_getprioceiling(pthread_mutexattr_t *attr, int *prioceiling){ if (attr == NULL) return pth_error(EINVAL, EINVAL); /* not supported */ return pth_error(ENOSYS, ENOSYS);}int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol){ if (attr == NULL) return pth_error(EINVAL, EINVAL); /* not supported */ return pth_error(ENOSYS, ENOSYS);}int pthread_mutexattr_getprotocol(pthread_mutexattr_t *attr, int *protocol){ if (attr == NULL) return pth_error(EINVAL, EINVAL); /* not supported */ return pth_error(ENOSYS, ENOSYS);}int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared){ if (attr == NULL) return pth_error(EINVAL, EINVAL); /* not supported */ return pth_error(ENOSYS, ENOSYS);}int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared){ if (attr == NULL) return pth_error(EINVAL, EINVAL); /* not supported */ return pth_error(ENOSYS, ENOSYS);}int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type){ if (attr == NULL) return pth_error(EINVAL, EINVAL); /* not supported */ return pth_error(ENOSYS, ENOSYS);}int pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *type){ if (attr == NULL) return pth_error(EINVAL, EINVAL); /* not supported */ return pth_error(ENOSYS, ENOSYS);}/*** MUTEX ROUTINES*/int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr){ pth_mutex_t *m; pthread_initialize(); if (mutex == NULL) return pth_error(EINVAL, EINVAL); if ((m = (pth_mutex_t *)malloc(sizeof(pth_mutex_t))) == NULL) return errno; if (!pth_mutex_init(m)) return errno; (*mutex) = (pthread_mutex_t)m; return OK;}int pthread_mutex_destroy(pthread_mutex_t *mutex){ if (mutex == NULL) return pth_error(EINVAL, EINVAL); free(*mutex); *mutex = NULL; return OK;}int pthread_mutex_setprioceiling(pthread_mutex_t *mutex, int prioceiling, int *old_ceiling){ if (mutex == NULL) return pth_error(EINVAL, EINVAL); if (*mutex == PTHREAD_MUTEX_INITIALIZER) if (pthread_mutex_init(mutex, NULL) != OK) return errno; /* not supported */ return pth_error(ENOSYS, ENOSYS);}int pthread_mutex_getprioceiling(pthread_mutex_t *mutex, int *prioceiling){ if (mutex == NULL) return pth_error(EINVAL, EINVAL); if (*mutex == PTHREAD_MUTEX_INITIALIZER) if (pthread_mutex_init(mutex, NULL) != OK) return errno; /* not supported */ return pth_error(ENOSYS, ENOSYS);}int pthread_mutex_lock(pthread_mutex_t *mutex){ if (mutex == NULL) return pth_error(EINVAL, EINVAL); if (*mutex == PTHREAD_MUTEX_INITIALIZER) if (pthread_mutex_init(mutex, NULL) != OK) return errno; if (!pth_mutex_acquire((pth_mutex_t *)(*mutex), FALSE, NULL)) return errno; return OK;}int pthread_mutex_trylock(pthread_mutex_t *mutex){ if (mutex == NULL) return pth_error(EINVAL, EINVAL); if (*mutex == PTHREAD_MUTEX_INITIALIZER) if (pthread_mutex_init(mutex, NULL) != OK) return errno; if (!pth_mutex_acquire((pth_mutex_t *)(*mutex), TRUE, NULL)) return errno; return OK;}int pthread_mutex_unlock(pthread_mutex_t *mutex){ if (mutex == NULL) return pth_error(EINVAL, EINVAL); if (*mutex == PTHREAD_MUTEX_INITIALIZER) if (pthread_mutex_init(mutex, NULL) != OK) return errno; if (!pth_mutex_release((pth_mutex_t *)(*mutex))) return errno; return OK;}/*** LOCK ATTRIBUTE ROUTINES*/int pthread_rwlockattr_init(pthread_rwlockattr_t *attr){ pthread_initialize(); if (attr == NULL) return pth_error(EINVAL, EINVAL); /* nothing to do for us */ return OK;}int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr){ if (attr == NULL) return pth_error(EINVAL, EINVAL); /* nothing to do for us */ return OK;}int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared){ if (attr == NULL) return pth_error(EINVAL, EINVAL); /* not supported */ return pth_error(ENOSYS, ENOSYS);}int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr, int *pshared){ if (attr == NULL) return pth_error(EINVAL, EINVAL); /* not supported */ return pth_error(ENOSYS, ENOSYS);}/*** LOCK ROUTINES*/int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr){ pth_rwlock_t *rw; pthread_initialize(); if (rwlock == NULL) return pth_error(EINVAL, EINVAL); if ((rw = (pth_rwlock_t *)malloc(sizeof(pth_rwlock_t))) == NULL) return errno; if (!pth_rwlock_init(rw)) return errno; (*rwlock) = (pthread_rwlock_t)rw; return OK;}int pthread_rwlock_destroy(pthread_rwlock_t *rwlock){ if (rwlock == NULL) return pth_error(EINVAL, EINVAL); free(*rwlock); *rwlock = NULL; return OK;}int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock){ if (rwlock == NULL) return pth_error(EINVAL, EINVAL); if (*rwlock == PTHREAD_RWLOCK_INITIALIZER) if (pthread_rwlock_init(rwlock, NULL) != OK) return errno; if (!pth_rwlock_acquire((pth_rwlock_t *)(*rwlock), PTH_RWLOCK_RD, FALSE, NULL)) return errno; return OK;}int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock){ if (rwlock == NULL) return pth_error(EINVAL, EINVAL); if (*rwlock == PTHREAD_RWLOCK_INITIALIZER) if (pthread_rwlock_init(rwlock, NULL) != OK) return errno; if (!pth_rwlock_acquire((pth_rwlock_t *)(*rwlock), PTH_RWLOCK_RD, TRUE, NULL)) return errno; return OK;}int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock){ if (rwlock == NULL) return pth_error(EINVAL, EINVAL); if (*rwlock == PTHREAD_RWLOCK_INITIALIZER) if (pthread_rwlock_init(rwlock, NULL) != OK) return errno; if (!pth_rwlock_acquire((pth_rwlock_t *)(*rwlock), PTH_RWLOCK_RW, FALSE, NULL)) return errno; return OK;}int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock){ if (rwlock == NULL) return pth_error(EINVAL, EINVAL); if (*rwlock == PTHREAD_RWLOCK_INITIALIZER) if (pthread_rwlock_init(rwlock, NULL) != OK) return errno; if (!pth_rwlock_acquire((pth_rwlock_t *)(*rwlock), PTH_RWLOCK_RW, TRUE, NULL)) return errno; return OK;}int pthread_rwlock_unlock(pthread_rwlock_t *rwlock){ if (rwlock == NULL) return pth_error(EINVAL, EINVAL); if (*rwlock == PTHREAD_RWLOCK_INITIALIZER) if (pthread_rwlock_init(rwlock, NULL) != OK) return errno; if (!pth_rwlock_release((pth_rwlock_t *)(*rwlock))) return errno; return OK;}/*** CONDITION ATTRIBUTE ROUTINES*/int pthread_condattr_init(pthread_condattr_t *attr){ pthread_initialize(); if (attr == NULL) return pth_error(EINVAL, EINVAL); /* nothing to do for us */ return OK;}int pthread_condattr_destroy(pthread_condattr_t *attr){ if (attr == NULL) return pth_error(EINVAL, EINVAL); /* nothing to do for us */ return OK;}int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared){ if (attr == NULL) return pth_error(EINVAL, EINVAL); /* not supported */ return pth_error(ENOSYS, ENOSYS);}int pthread_condattr_getpshared(pthread_condattr_t *attr, int *pshared){ if (attr == NULL) return pth_error(EINVAL, EINVAL); /* not supported */ return pth_error(ENOSYS, ENOSYS);}/*** CONDITION ROUTINES*/int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr){ pth_cond_t *cn; pthread_initialize(); if (cond == NULL) return pth_error(EINVAL, EINVAL); if ((cn = (pth_cond_t *)malloc(sizeof(pth_cond_t))) == NULL) return errno; if (!pth_cond_init(cn)) return errno; (*cond) = (pthread_cond_t)cn; return OK;}int pthread_cond_destroy(pthread_cond_t *cond){ if (cond == NULL) return pth_error(EINVAL, EINVAL); free(*cond); *cond = NULL; return OK;}int pthread_cond_broadcast(pthread_cond_t *cond){ if (cond == NULL) return pth_error(EINVAL, EINVAL); if (*cond == PTHREAD_COND_INITIALIZER) if (pthread_cond_init(cond, NULL) != OK) return errno; if (!pth_cond_notify((pth_cond_t *)(*cond), TRUE)) return errno; return OK;}int pthread_cond_signal(pthread_cond_t *cond){ if (cond == NULL) return pth_error(EINVAL, EINVAL); if (*cond == PTHREAD_COND_INITIALIZER) if (pthread_cond_init(cond, NULL) != OK) return errno; if (!pth_cond_notify((pth_cond_t *)(*cond), FALSE)) return errno; return OK;}int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex){ if (cond == NULL || mutex == NULL) return pth_error(EINVAL, EINVAL); if (*cond == PTHREAD_COND_INITIALIZER) if (pthread_cond_init(cond, NULL) != OK) return errno; if (*mutex == PTHREAD_MUTEX_INITIALIZER) if (pthread_mutex_init(mutex, NULL) != OK) return errno; if (!pth_cond_await((pth_cond_t *)(*cond), (pth_mutex_t *)(*mutex), NULL)) return errno; return OK;}int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime){ pth_event_t ev; static pth_key_t ev_key = PTH_KEY_INIT; if (cond == NULL || mutex == NULL || abstime == NULL) return pth_error(EINVAL, EINVAL);#ifdef __amigaos__ if (abstime->ts_sec < 0 || abstime->ts_nsec < 0 || abstime->ts_nsec >= 1000000000)#else if (abstime->tv_sec < 0 || abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)#endif return pth_error(EINVAL, EINVAL); if (*cond == PTHREAD_COND_INITIALIZER) if (pthread_cond_init(cond, NULL) != OK) return errno; if (*mutex == PTHREAD_MUTEX_INITIALIZER) if (pthread_mutex_init(mutex, NULL) != OK) return errno; ev = pth_event(PTH_EVENT_TIME|PTH_MODE_STATIC, &ev_key,#ifdef __amigaos__ pth_time(abstime->ts_sec, (abstime->ts_nsec)/1000)#else pth_time(abstime->tv_sec, (abstime->tv_nsec)/1000)#endif ); if (!pth_cond_await((pth_cond_t *)(*cond), (pth_mutex_t *)(*mutex), ev)) return errno; if (pth_event_status(ev) == PTH_STATUS_OCCURRED) return ETIMEDOUT; return OK;}/*** POSIX 1003.1j*/int pthread_abort(pthread_t thread){ if (!pth_abort((pth_t)thread)) return errno; return OK;}/*** THREAD-SAFE REPLACEMENT FUNCTIONS*/pid_t __pthread_fork(void){ pthread_initialize(); return pth_fork();}unsigned int __pthread_sleep(unsigned int sec){ pthread_initialize(); return pth_sleep(sec);}int __pthread_system(const char *cmd){ pthread_initialize(); return pth_system(cmd);}int __pthread_nanosleep(const struct timespec *rqtp, struct timespec *rmtp){ pthread_initialize(); return pth_nanosleep(rqtp, rmtp);}int __pthread_usleep(unsigned int sec){ pthread_initialize(); return pth_usleep(sec);}int __pthread_sigwait(const sigset_t *set, int *sig){ pthread_initialize(); return pth_sigwait(set, sig);}pid_t __pthread_waitpid(pid_t pid, int *status, int options){ pthread_initialize(); return pth_waitpid(pid, status, options);}int __pthread_connect(int s, struct sockaddr *addr, socklen_t addrlen){ pthread_initialize(); return pth_connect(s, addr, addrlen);}int __pthread_accept(int s, struct sockaddr *addr, socklen_t *addrlen){ pthread_initialize(); return pth_accept(s, addr, addrlen);}int __pthread_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout){ pthread_initialize(); return pth_select(nfds, readfds, writefds, exceptfds, timeout);}int __pthread_poll(struct pollfd *pfd, nfds_t nfd, int timeout){ pthread_initialize(); return pth_poll(pfd, nfd, timeout);}ssize_t __pthread_read(int fd, void *buf, size_t nbytes){ pthread_initialize(); return pth_read(fd, buf, nbytes);}ssize_t __pthread_write(int fd, const void *buf, size_t nbytes){ pthread_initialize(); return pth_write(fd, buf, nbytes);}ssize_t __pthread_readv(int fd, const struct iovec *piovec, int iocnt){ pthread_initialize(); return pth_readv(fd, piovec, iocnt);}ssize_t __pthread_writev(int fd, const struct iovec *piovec, int iocnt){ pthread_initialize(); return pth_writev(fd, piovec, iocnt);}ssize_t __pthread_recv(int fd, void *buf, size_t nbytes, int flags){ pthread_initialize(); return pth_recv(fd, buf, nbytes, flags);}ssize_t __pthread_send(int fd, const void *buf, size_t nbytes, int flags){ pthread_initialize(); return pth_send(fd, buf, nbytes, flags);}ssize_t __pthread_recvfrom(int fd, void *buf, size_t nbytes, int flags, struct sockaddr *from, socklen_t *fromlen){ pthread_initialize(); return pth_recvfrom(fd, buf, nbytes, flags, from, fromlen);}ssize_t __pthread_sendto(int fd, const void *buf, size_t nbytes, int flags, const struct sockaddr *to, socklen_t tolen){ pthread_initialize(); return pth_sendto(fd, buf, nbytes, flags, to, tolen);}ssize_t __pthread_pread(int fd, void *buf, size_t nbytes, off_t offset){ pthread_initialize(); return pth_pread(fd, buf, nbytes, offset);}ssize_t __pthread_pwrite(int fd, const void *buf, size_t nbytes, off_t offset){ pthread_initialize(); return pth_pwrite(fd, buf, nbytes, offset);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -