📄 pthreadlib.c
字号:
* feature. */ _pthread_setcanceltype = (FUNCPTR) pthread_setcanceltype; pthreadLibInited = TRUE; } }/* * Section 3 - Process Primitives *//********************************************************************************* pthread_sigmask - change and/or examine calling thread's signal mask (POSIX)** This routine changes the signal mask for the calling thread as described* by the <how> and <set> arguments. If <oset> is not NULL, the previous* signal mask is stored in the location pointed to by it.** The value of <how> indicates the manner in which the set is changed and* consists of one of the following defined in 'signal.h':* .iP SIG_BLOCK 4* The resulting set is the union of the current set and the signal set* pointed to by <set>.* .iP SIG_UNBLOCK* The resulting set is the intersection of the current set and the complement* of the signal set pointed to by <set>.* .iP SIG_SETMASK* The resulting set is the signal set pointed to by <oset>.* .LP** RETURNS: On success zero; on failure a non-zero error code is returned.** ERRNOS: 'EINVAL'** SEE ALSO: kill(), pthread_kill(), sigprocmask(), sigaction(),* sigsuspend(), sigwait()*/int pthread_sigmask ( int how, /* method for changing set */ const sigset_t * set, /* new set of signals */ sigset_t * oset /* old set of signals */ ) { if (sigprocmask(how, (sigset_t *) set, oset) == ERROR) return (errno); return (_RETURN_PTHREAD_SUCCESS); }/********************************************************************************* pthread_kill - send a signal to a thread (POSIX)** This routine sends signal number <sig> to the thread specified by <thread>.* The signal is delivered and handled as described for the kill() function.** RETURNS: On success zero; on failure a non-zero error code.** ERRNOS: 'ESRCH', 'EINVAL'** SEE ALSO: kill(), pthread_sigmask(), sigprocmask(), sigaction(),* sigsuspend(), sigwait()*/int pthread_kill ( pthread_t thread, /* thread to signal */ int sig /* signal to send */ ) { internalPthread * pThread = (internalPthread *)thread; WIND_TCB * pTcb; if (!VALID_PTHREAD(pThread, pTcb)) return (ESRCH); if (sig < 0 || sig >_NSIGS) return (EINVAL); if (sig == 0 || kill(((int)pThread->taskId), sig) == OK) return (_RETURN_PTHREAD_SUCCESS); else return (errno); }/* * Section 11.3 - Mutexes *//********************************************************************************* pthread_mutexattr_init - initialize mutex attributes object (POSIX)** This routine initializes the mutex attribute object <pAttr> and fills it* with default values for the attributes as defined by the POSIX* specification.** RETURNS: On success zero; on failure a non-zero error code.** ERRNOS: 'EINVAL'** SEE ALSO:* pthread_mutexattr_destroy(),* pthread_mutexattr_getprioceiling(),* pthread_mutexattr_getprotocol(),* pthread_mutexattr_setprioceiling(),* pthread_mutexattr_setprotocol(),* pthread_mutex_init()*/int pthread_mutexattr_init ( pthread_mutexattr_t *pAttr /* mutex attributes */ ) { if (!pAttr) return (EINVAL); pAttr->mutexAttrStatus = PTHREAD_INITIALIZED; return (_RETURN_PTHREAD_SUCCESS); }/********************************************************************************* pthread_mutexattr_destroy - destroy mutex attributes object (POSIX)** This routine destroys a mutex attribute object. The mutex attribute object* must not be reused until it is reinitialized.** RETURNS: On success zero; on failure a non-zero error code.** ERRNOS: 'EINVAL'** SEE ALSO:* pthread_mutexattr_getprioceiling(),* pthread_mutexattr_getprotocol(),* pthread_mutexattr_init(),* pthread_mutexattr_setprioceiling(),* pthread_mutexattr_setprotocol(),* pthread_mutex_init()*/int pthread_mutexattr_destroy ( pthread_mutexattr_t *pAttr /* mutex attributes */ ) { if (!pAttr) return (EINVAL); pAttr->mutexAttrStatus = PTHREAD_DESTROYED; return (_RETURN_PTHREAD_SUCCESS); }/********************************************************************************* pthread_mutexattr_setprotocol - set protocol attribute in mutex attribut object (POSIX)** This function selects the locking protocol to be used when a mutex is created* using this attributes object. The protocol to be selected is either* PTHREAD_PRIO_INHERIT or PTHREAD_PRIO_PROTECT.** RETURNS: On success zero; on failure a non-zero error code.** ERRNOS: 'EINVAL', 'ENOTSUP'** SEE ALSO:* pthread_mutexattr_destroy(),* pthread_mutexattr_getprioceiling(),* pthread_mutexattr_getprotocol(),* pthread_mutexattr_init(),* pthread_mutexattr_setprioceiling(),* pthread_mutex_init()*/int pthread_mutexattr_setprotocol ( pthread_mutexattr_t *pAttr, /* mutex attributes */ int protocol /* new protocol */ ) { if (!pAttr || pAttr->mutexAttrStatus != PTHREAD_INITIALIZED) return (EINVAL); if (protocol != PTHREAD_PRIO_NONE && protocol != PTHREAD_PRIO_INHERIT && protocol != PTHREAD_PRIO_PROTECT) { return (ENOTSUP); } pAttr->mutexAttrProtocol = protocol; return (_RETURN_PTHREAD_SUCCESS); }/********************************************************************************* pthread_mutexattr_getprotocol - get value of protocol in mutex attributes object (POSIX)** This function gets the current value of the protocol attribute in a mutex* attributes object.** RETURNS: On success zero; on failure a non-zero error code.** ERRNOS: 'EINVAL'** SEE ALSO:* pthread_mutexattr_destroy(),* pthread_mutexattr_getprioceiling(),* pthread_mutexattr_init(),* pthread_mutexattr_setprioceiling(),* pthread_mutexattr_setprotocol(),* pthread_mutex_init()*/int pthread_mutexattr_getprotocol ( pthread_mutexattr_t *pAttr, /* mutex attributes */ int *pProtocol /* current protocol (out) */ ) { if (!pAttr || pAttr->mutexAttrStatus != PTHREAD_INITIALIZED) return (EINVAL); *pProtocol = pAttr->mutexAttrProtocol; return (_RETURN_PTHREAD_SUCCESS); }/********************************************************************************* pthread_mutexattr_setprioceiling - set prioceiling attribute in mutex attributes object (POSIX)** This function sets the value of the prioceiling attribute in a mutex* attributes object. Unless the protocol attribute is set to* PTHREAD_PRIO_PROTECT, this attribute is ignored.** RETURNS: On success zero; on failure a non-zero error code.** ERRNOS: 'EINVAL'** SEE ALSO:* pthread_mutexattr_destroy(),* pthread_mutexattr_getprioceiling(),* pthread_mutexattr_getprotocol(),* pthread_mutexattr_init(),* pthread_mutexattr_setprotocol(),* pthread_mutex_init()*/int pthread_mutexattr_setprioceiling ( pthread_mutexattr_t *pAttr, /* mutex attributes */ int prioceiling /* new priority ceiling */ ) { if (!pAttr || pAttr->mutexAttrStatus != PTHREAD_INITIALIZED || (prioceiling < PRIO_LOWER_BOUND || prioceiling > PRIO_UPPER_BOUND)) { return (EINVAL); } pAttr->mutexAttrPrioceiling = prioceiling; return (_RETURN_PTHREAD_SUCCESS); }/********************************************************************************* pthread_mutexattr_getprioceiling - get the current value of the prioceiling attribute in a mutex attributes object (POSIX)** This function gets the current value of the prioceiling attribute in a mutex* attributes object. Unless the value of the protocol attribute is* PTHREAD_PRIO_PROTECT, this value is ignored.** RETURNS: On success zero; on failure a non-zero error code.** ERRNOS: 'EINVAL'** SEE ALSO:* pthread_mutexattr_destroy(),* pthread_mutexattr_getprotocol(),* pthread_mutexattr_init(),* pthread_mutexattr_setprioceiling(),* pthread_mutexattr_setprotocol(),* pthread_mutex_init()*/int pthread_mutexattr_getprioceiling ( pthread_mutexattr_t *pAttr, /* mutex attributes */ int *pPrioceiling /* current priority ceiling (out) */ ) { if (!pAttr || pAttr->mutexAttrStatus != PTHREAD_INITIALIZED) return (EINVAL); *pPrioceiling = pAttr->mutexAttrPrioceiling; return (_RETURN_PTHREAD_SUCCESS); }/********************************************************************************* pthread_mutex_getprioceiling - get the value of the prioceiling attribute of a mutex (POSIX)** This function gets the current value of the prioceiling attribute of a mutex.* Unless the mutex was created with a protocol attribute value of* PTHREAD_PRIO_PROTECT, this value is meaningless.** RETURNS: On success zero; on failure a non-zero error code.** ERRNOS: 'EINVAL'** SEE ALSO:* pthread_mutex_setprioceiling(),* pthread_mutexattr_getprioceiling(),* pthread_mutexattr_setprioceiling()*/int pthread_mutex_getprioceiling ( pthread_mutex_t *pMutex, /* POSIX mutex */ int *pPrioceiling /* current priority ceiling (out) */ ) { if (!pMutex || pMutex->mutexValid != TRUE) return (EINVAL); INIT_MUTEX(pMutex); *pPrioceiling = pMutex->mutexAttr.mutexAttrPrioceiling; return (_RETURN_PTHREAD_SUCCESS); }/********************************************************************************* pthread_mutex_setprioceiling - dynamically set the prioceiling attribute of a mutex (POSIX)** This function dynamically sets the value of the prioceiling attribute of a* mutex. Unless the mutex was created with a protocol value of* PTHREAD_PRIO_PROTECT, this function does nothing.** RETURNS: On success zero; on failure a non-zero error code.** ERRNOS: 'EINVAL', 'EPERM', S_objLib_OBJ_ID_ERROR, S_semLib_NOT_ISR_CALLABLE** SEE ALSO:* pthread_mutex_getprioceiling(),* pthread_mutexattr_getprioceiling(),* pthread_mutexattr_setprioceiling()*/int pthread_mutex_setprioceiling ( pthread_mutex_t *pMutex, /* POSIX mutex */ int prioceiling, /* new priority ceiling */ int *pOldPrioceiling /* old priority ceiling (out) */ ) { if (!pMutex || pMutex->mutexValid != TRUE) return (EINVAL); INIT_MUTEX(pMutex); if (semTake(pMutex->mutexSemId, WAIT_FOREVER) == ERROR) return (EINVAL); /* internal error */ *pOldPrioceiling = pMutex->mutexAttr.mutexAttrPrioceiling; pMutex->mutexAttr.mutexAttrPrioceiling = prioceiling; /* Clear errno before trying to test it */ errno = 0; if (semGive(pMutex->mutexSemId) == ERROR) { if (errno == S_semLib_INVALID_OPERATION) return (EPERM); /* not owner - can't happen */ else return (errno); /* some other error */ } return (_RETURN_PTHREAD_SUCCESS); }/********************************************************************************* pthread_mutex_init - initialize mutex from attributes object (POSIX)** This routine initializes the mutex object pointed to by <pMutex> according* to the mutex attributes specified in <pAttr>. If <pAttr> is NULL, default* attributes are used as defined in the POSIX specification.** RETURNS: On success zero; on failure a non-zero error code.** ERRNOS: 'EINVAL', 'EBUSY'** SEE ALSO:* semLib, semMLib,* pthread_mutex_destroy(),* pthread_mutex_lock(),* pthread_mutex_trylock(),* pthread_mutex_unlock(),* pthread_mutexattr_init(),* semMCreate()*/int pthread_mutex_init ( pthread_mutex_t *pMutex, /* POSIX mutex */ const pthread_mutexattr_t *pAttr /* mutex attributes */ ) { pthread_mutexattr_t *pSource; if (!pMutex) return (EINVAL); if (!pthreadLibInited) return (EINVAL); if (pMutex->mutexValid == TRUE) return (EBUSY); pSource = pAttr ? (pthread_mutexattr_t *)pAttr : &defaultMutexAttr; bcopy((const char *)pSource, (char *)&pMutex->mutexAttr, sizeof (pthread_mutexattr_t)); pMutex->mutexValid = TRUE; pMutex->mutexInitted = FALSE; pMutex->mutexIniting = 0; pMutex->mutexCondRefCount = 0; return (_RETURN_PTHREAD_SUCCESS); }/********************************************************************************* pthread_mutex_destroy - destroy a mutex (POSIX)** This routine destroys a mutex object, freeing the resources it might hold.* The mutex must be unlocked when this function is called, otherwise it will* return an error ('EBUSY').** RETURNS: On success zero; on failure a non-zero error code.** ERRNOS: 'EINVAL', 'EBUSY'** SEE ALSO:* semLib, semMLib,* pthread_mutex_init(),* pthread_mutex_lock(),* pthread_mutex_trylock(),* pthread_mutex_unlock(),* pthread_mutexattr_init(),* semDelete()*/int pthread_mutex_destroy
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -