本文整理汇总了C++中pthread_mutexattr_settype函数的典型用法代码示例。如果您正苦于以下问题:C++ pthread_mutexattr_settype函数的具体用法?C++ pthread_mutexattr_settype怎么用?C++ pthread_mutexattr_settype使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pthread_mutexattr_settype函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: vcos_pthreads_timer_create
VCOS_STATUS_T vcos_pthreads_timer_create(VCOS_TIMER_T *timer,
const char *name,
void (*expiration_routine)(void *context),
void *context)
{
pthread_mutexattr_t lock_attr;
VCOS_STATUS_T result = VCOS_SUCCESS;
int settings_changed_initialized = 0;
int lock_attr_initialized = 0;
int lock_initialized = 0;
(void)name;
vcos_assert(timer);
vcos_assert(expiration_routine);
memset(timer, 0, sizeof(VCOS_TIMER_T));
timer->orig_expiration_routine = expiration_routine;
timer->orig_context = context;
/* Create conditional variable for notifying the timer's thread
* when settings change.
*/
if (result == VCOS_SUCCESS)
{
int rc = pthread_cond_init(&timer->settings_changed, NULL);
if (rc == 0)
settings_changed_initialized = 1;
else
result = vcos_pthreads_map_error(rc);
}
/* Create attributes for the lock (we want it to be recursive) */
if (result == VCOS_SUCCESS)
{
int rc = pthread_mutexattr_init(&lock_attr);
if (rc == 0)
{
pthread_mutexattr_settype(&lock_attr, PTHREAD_MUTEX_RECURSIVE);
lock_attr_initialized = 1;
}
else
{
result = vcos_pthreads_map_error(rc);
}
}
/* Create lock for the timer structure */
if (result == VCOS_SUCCESS)
{
int rc = pthread_mutex_init(&timer->lock, &lock_attr);
if (rc == 0)
lock_initialized = 1;
else
result = vcos_pthreads_map_error(rc);
}
/* Lock attributes are no longer needed */
if (lock_attr_initialized)
pthread_mutexattr_destroy(&lock_attr);
/* Create the underlying thread */
if (result == VCOS_SUCCESS)
{
int rc = pthread_create(&timer->thread, NULL, _timer_thread, timer);
if (rc != 0)
result = vcos_pthreads_map_error(rc);
}
/* Clean up if anything went wrong */
if (result != VCOS_SUCCESS)
{
if (lock_initialized)
pthread_mutex_destroy(&timer->lock);
if (settings_changed_initialized)
pthread_cond_destroy(&timer->settings_changed);
}
return result;
}
示例2: file
//.........这里部分代码省略.........
//
smControl->sharedMemorySegmentSize = sharedMemorySegmentSize;
smControl->currentOffset = smSize;
smControl->currentSize = sharedMemorySegmentSize - smSize;
smControl->systemInitialized = 0;
//
// Create the mutex attribute initializer that we can use to initialize
// all of the mutexes in the B-trees.
//
pthread_mutexattr_t* mutexAttributes = &smControl->masterMutexAttributes;
status = pthread_mutexattr_init ( mutexAttributes );
if ( status != 0 )
{
printf ( "Unable to initialize mutex attributes - errno: %u[%m].\n",
status );
return 0;
}
//
// Set this mutex to be a process shared mutex.
//
status = pthread_mutexattr_setpshared ( mutexAttributes,
PTHREAD_PROCESS_SHARED );
if ( status != 0 )
{
printf ( "Unable to set shared mutex attribute - errno: %u[%m].\n",
status );
return 0;
}
//
// Set this mutex to be a recursive mutex.
//
status = pthread_mutexattr_settype ( mutexAttributes,
PTHREAD_MUTEX_RECURSIVE );
if ( status != 0 )
{
printf ( "Unable to set recursive mutex attribute - errno: %u[%m].\n",
status );
return 0;
}
//
// Create the condition variable initializer that we can use to
// initialize all of the condition variables in the shared memory
// segment.
//
pthread_condattr_t* conditionVariableAttributes =
&smControl->masterCvAttributes;
status = pthread_condattr_init ( conditionVariableAttributes );
if ( status != 0 )
{
printf ( "Unable to initialize condition variable attributes - "
"errno: %u[%m].\n", status );
return 0;
}
//
// Set this condition variable to be shared between processes.
//
status = pthread_condattr_setpshared ( conditionVariableAttributes,
PTHREAD_PROCESS_SHARED );
if ( status != 0 )
{
printf ( "Unable to set shared condition variable attributes - "
"errno: %u[%m].\n", status );
示例3: open_app_log
void open_app_log() {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex, &attr);
}
示例4: arenaAction
local void arenaAction(Arena *arena, int action)
{
TurfStats *ts, **p_ts = P_ARENA_DATA(arena, tskey);
if (action == AA_PRECREATE)
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init((pthread_mutex_t*)P_ARENA_DATA(arena, mtxkey), &attr);
pthread_mutexattr_destroy(&attr);
}
else if (action == AA_PRECREATE)
{
ts = amalloc(sizeof(TurfStats));
*p_ts = ts;
}
ts = *p_ts;
LOCK_STATUS(arena);
if (action == AA_CREATE)
{
ConfigHandle c = arena->cfg;
ts->maxHistory = config->GetInt(c, "TurfStats", "MaxHistory", 0);
if (ts->maxHistory<0)
ts->maxHistory = 0;
ts->statsOnDing = config->GetInt(c, "TurfStats", "StatsOnDing", 1);
if (ts->statsOnDing<1)
ts->statsOnDing=1;
ts->dingCount = 0;
ts->numStats = 0;
LLInit(&ts->stats); /* initalize list of stats */
}
else if (action == AA_DESTROY)
{
/* free history array */
clearHistory(arena);
/* might as well, just in case to be safe */
ts->numStats = 0;
ts->dingCount = 0;
}
else if (action == AA_CONFCHANGED)
{
int newMaxHistory;
ConfigHandle c = arena->cfg;
ts->statsOnDing = config->GetInt(c, "TurfStats", "StatsOnDing", 1);
if (ts->statsOnDing<1)
ts->statsOnDing=1;
newMaxHistory = config->GetInt(c, "TurfStats", "MaxHistory", 0);
if (newMaxHistory < 0)
newMaxHistory = 0; /* max history must be >= 0 */
if (newMaxHistory != ts->maxHistory)
{
ts->maxHistory = newMaxHistory;
/* erase history */
clearHistory(arena);
}
}
UNLOCK_STATUS(arena);
if (action == AA_DESTROY)
{
afree(ts);
}
else if (action == AA_POSTDESTROY)
{
pthread_mutex_destroy((pthread_mutex_t*)P_ARENA_DATA(arena, mtxkey));
}
}
示例5: main
int main (int argc, char *argv[])
{
#ifdef PTHREADS
int ret;
pthread_attr_t attr;
pthread_t tid1, tid2, tid3;
pthread_mutexattr_t Attr;
pthread_mutexattr_init(&Attr);
pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_ERRORCHECK);
if (pthread_mutex_init(&mutexsum, &Attr)) {
printf("Error while using pthread_mutex_init\n");
}
#endif /* PTHREADS */
#ifdef TAU_MPI
int rc = MPI_SUCCESS;
#if defined(PTHREADS)
rc = MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
printf("MPI_Init_thread: provided = %d, MPI_THREAD_MULTIPLE=%d\n", provided, MPI_THREAD_MULTIPLE);
#elif defined(TAU_OPENMP)
rc = MPI_Init_thread(&argc, &argv, MPI_THREAD_FUNNELED, &provided);
printf("MPI_Init_thread: provided = %d, MPI_THREAD_FUNNELED=%d\n", provided, MPI_THREAD_FUNNELED);
#else
rc = MPI_Init(&argc, &argv);
#endif /* THREADS */
if (rc != MPI_SUCCESS) {
char *errorstring;
int length = 0;
MPI_Error_string(rc, errorstring, &length);
printf("Error: MPI_Init failed, rc = %d\n%s\n", rc, errorstring);
exit(1);
}
#endif /* TAU_MPI */
#ifdef PTHREADS
if (ret = pthread_create(&tid1, NULL, threaded_func, NULL) )
{
printf("Error: pthread_create (1) fails ret = %d\n", ret);
exit(1);
}
if (ret = pthread_create(&tid2, NULL, threaded_func, NULL) )
{
printf("Error: pthread_create (2) fails ret = %d\n", ret);
exit(1);
}
if (ret = pthread_create(&tid3, NULL, threaded_func, NULL) )
{
printf("Error: pthread_create (3) fails ret = %d\n", ret);
exit(1);
}
#endif /* PTHREADS */
/* On thread 0: */
int i;
//for (i = 0 ; i < 100 ; i++) {
do_work();
//}
#ifdef PTHREADS
if (ret = pthread_join(tid1, NULL) )
{
printf("Error: pthread_join (1) fails ret = %d\n", ret);
exit(1);
}
if (ret = pthread_join(tid2, NULL) )
{
printf("Error: pthread_join (2) fails ret = %d\n", ret);
exit(1);
}
if (ret = pthread_join(tid3, NULL) )
{
printf("Error: pthread_join (3) fails ret = %d\n", ret);
exit(1);
}
pthread_mutex_destroy(&mutexsum);
#endif /* PTHREADS */
#ifdef TAU_MPI
MPI_Finalize();
#endif /* TAU_MPI */
printf ("Done.\n");
return 0;
}
示例6: QMutex
GroupManager::GroupManager(NdnMediaProcess *pNdnMediaPro) {
ruMutex = new QMutex(QMutex::Recursive);
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&gm_mutex, &attr);
pGroupManager = this;
this->pNdnMediaPro = pNdnMediaPro;
QDomDocument settings;
QString configFileName = QDir::homePath() + "/" + \
".actd" + "/" + ".config";
QFile config(configFileName);
if (!config.exists())
critical("Config file does not exist!");
if (!config.open(QIODevice::ReadOnly))
critical("Can not open config file!");
if (!settings.setContent(&config)) {
config.close();
critical("can not parse config file!");
}
config.close();
QDomElement docElem = settings.documentElement(); //<config>
QDomNode n = docElem.firstChild();
isPrivate = false;
QString qsConfKey;
QString qsSessionKey;
QByteArray qbaSK;
while(!n.isNull()) {
if (n.nodeName() == "prefix") {
prefix = n.toElement().text();
} else if (n.nodeName() == "confName") {
confName = n.toElement().text();
} else if (n.nodeName() == "channelName") {
} else if (n.nodeName() == "private") {
QString p = n.toElement().text();
if (p == "true")
isPrivate = true;
else
isPrivate = false;
} else if (n.nodeName() == "confKey" && isPrivate) {
qsConfKey = n.toElement().text();
QByteArray qbaCK = \
QByteArray::fromBase64(qsConfKey.toLocal8Bit());
memcpy(conferenceKey, qbaCK.data(), qbaCK.size());
} else if (n.nodeName() == "sessionKey" && isPrivate) {
qsSessionKey = n.toElement().text();
qbaSK = \
QByteArray::fromBase64(qsSessionKey.toLocal8Bit());
} else {
debug("unknown atrribute in config file!");
}
n = n.nextSibling();
}
if (confName.isEmpty())
critical("no confName in config!");
if (isPrivate && (qsConfKey.isEmpty() || qsSessionKey.isEmpty()))
isPrivate = false;
if (isPrivate) {
pNdnMediaPro->setPrivate();
pNdnMediaPro->setSK(qbaSK);
}
ndnContext();
localUser = NULL;
// ui_session id for remoteUsers
// 5000 at maximum
for (int i = 5001; i < 10000; ++i)
qqRSesIds.enqueue(i);
}
示例7: pthread_mutexattr_init
#include "OperateDiskProc.h"
#include "transfers_base.h"
#include "netManager.h"
#include "LOGCTRL.h"
//#define NO_POS_DEBUG
#include "pos_debug.h"
static INT8 tmpBuff[MAX_SEND_BUF_LEN];
#if SKJ_PTHREAD_KPMUTEX_OPEN == 1
pthread_mutex_t *g_skjkpmutex = new pthread_mutex_t;
pthread_mutexattr_t skjmattr;
int skjret = pthread_mutexattr_init(&skjmattr);
int skjret1 = pthread_mutexattr_settype(&skjmattr, PTHREAD_MUTEX_RECURSIVE_NP);
int skjret2 = pthread_mutex_init(g_skjkpmutex, &skjmattr);
int skjret3 = pthread_mutexattr_destroy(&skjmattr);
//static pthread_mutex_t g_skjkpmutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
#endif
CYWXmlBase::CYWXmlBase(CYWXML_GY &ywxml_gy):m_ywxml_gy(ywxml_gy)
{
m_ywlx = ywxml_gy.m_strID;
m_servip = "";
m_servport = "";
m_servpath = "";
m_pin = "";
m_Err = "";
m_nsrsbh = "";
示例8: mutex
mutex(int type = PTHREAD_MUTEX_DEFAULT) {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, type);
pthread_mutex_init(&_mutex, &attr);
}
示例9: pthread_sigmask
/* Test function -- This one calls pthread_mutex_trylock and check that no EINTR is returned. */
void *test(void *arg)
{
int ret = 0;
int i;
long pshared;
pthread_mutex_t mtx[NSCENAR + 2];
pthread_mutexattr_t ma[NSCENAR + 1];
/* We don't block the signals SIGUSR1 and SIGUSR2 for this THREAD */
ret = pthread_sigmask(SIG_UNBLOCK, &usersigs, NULL);
if (ret != 0) {
UNRESOLVED(ret,
"Unable to unblock SIGUSR1 and SIGUSR2 in worker thread");
}
/* System abilities */
pshared = sysconf(_SC_THREAD_PROCESS_SHARED);
/* Initialize the mutex objects according to the scenarii */
for (i = 0; i < NSCENAR; i++) {
ret = pthread_mutexattr_init(&ma[i]);
if (ret != 0) {
UNRESOLVED(ret,
"[parent] Unable to initialize the mutex attribute object");
}
#ifndef WITHOUT_XOPEN
/* Set the mutex type */
ret = pthread_mutexattr_settype(&ma[i], scenarii[i].m_type);
if (ret != 0) {
UNRESOLVED(ret, "[parent] Unable to set mutex type");
}
#endif
/* Set the pshared attributes, if supported */
if ((pshared > 0) && (scenarii[i].m_pshared != 0)) {
ret =
pthread_mutexattr_setpshared(&ma[i],
PTHREAD_PROCESS_SHARED);
if (ret != 0) {
UNRESOLVED(ret,
"[parent] Unable to set the mutex process-shared");
}
}
/* Initialize the mutex */
ret = pthread_mutex_init(&mtx[i], &ma[i]);
if (ret != 0) {
UNRESOLVED(ret, "[parent] Mutex init failed");
}
}
/* Default mutexattr object */
ret = pthread_mutexattr_init(&ma[i]);
if (ret != 0) {
UNRESOLVED(ret,
"[parent] Unable to initialize the mutex attribute object");
}
ret = pthread_mutex_init(&mtx[i], &ma[i]);
if (ret != 0) {
UNRESOLVED(ret, "[parent] Mutex init failed");
}
/* Default mutex */
ret = pthread_mutex_init(&mtx[i + 1], NULL);
if (ret != 0) {
UNRESOLVED(ret, "[parent] Mutex init failed");
}
/* do the real testing */
while (do_it) {
count_ope++;
ret = pthread_mutex_trylock(&mtx[count_ope % (NSCENAR + 2)]);
if (ret == EINTR) {
FAILED("EINTR was returned");
}
if (ret != 0) {
UNRESOLVED(ret, "1st trylock failed");
}
ret = pthread_mutex_trylock(&mtx[count_ope % (NSCENAR + 2)]);
if (ret == EINTR) {
FAILED("EINTR was returned");
}
if (ret == 0) {
ret =
pthread_mutex_unlock(&mtx
[count_ope % (NSCENAR + 2)]);
if (ret != 0) {
UNRESOLVED(ret, "Unlocking the mutex failed");
}
ret = EBUSY;
}
if (ret != EBUSY) {
UNRESOLVED(ret, "Unexpected error was returned.");
}
ret = pthread_mutex_unlock(&mtx[count_ope % (NSCENAR + 2)]);
if (ret != 0) {
UNRESOLVED(ret, "Failed to unlock the mutex");
}
//.........这里部分代码省略.........
示例10: recursive_mutex_test
void recursive_mutex_test(void)
{
pthread_t thread[NTHREADS];
#ifdef SDCC
pthread_addr_t result[NTHREADS];
pthread_attr_t attr;
#endif
pthread_mutexattr_t mattr;
int type;
int status;
int i;
/* Initialize the mutex attributes */
pthread_mutexattr_init(&mattr);
status = pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE);
if (status != 0)
{
printf("recursive_mutex_test: ERROR pthread_mutexattr_settype failed, status=%d\n", status);
}
status = pthread_mutexattr_gettype(&mattr, &type);
if (status != 0)
{
printf("recursive_mutex_test: ERROR pthread_mutexattr_gettype failed, status=%d\n", status);
}
if (type != PTHREAD_MUTEX_RECURSIVE)
{
printf("recursive_mutex_test: ERROR pthread_mutexattr_gettype return type=%d\n", type);
}
/* Initialize the mutex */
printf("recursive_mutex_test: Initializing mutex\n");
status = pthread_mutex_init(&mut, &mattr);
if (status != 0)
{
printf("recursive_mutex_test: ERROR pthread_mutex_init failed, status=%d\n", status);
}
/* Start the threads -- all at the same, default priority */
for (i = 0; i < NTHREADS; i++)
{
printf("recursive_mutex_test: Starting thread %d\n", i+1);
#ifdef SDCC
(void)pthread_attr_init(&attr);
status = pthread_create(&thread[i], &attr, thread_outer, (pthread_addr_t)i+1);
#else
status = pthread_create(&thread[i], NULL, thread_outer, (pthread_addr_t)i+1);
#endif
if (status != 0)
{
printf("recursive_mutex_test: ERROR thread#%d creation: %d\n", i+1, status);
}
}
/* Wait for all; of the threads to complete */
for (i = 0; i < NTHREADS; i++)
{
printf("recursive_mutex_test: Waiting for thread %d\n", i+1);
#ifdef SDCC
pthread_join(thread[i], &result[i]);
#else
pthread_join(thread[i], NULL);
#endif
}
printf("recursive_mutex_test: Complete\n");
}
示例11: pthread_mutexattr_init
TInt CTestSemtrywait::TestSem392( )
{
int errsum=0, err = 0;
int retval = 0;
ThreadData lThreadData;
sem_t lSignalSemaphore;
sem_t lSuspendSemaphore;
sem_t lTestSemaphore;
pthread_mutex_t lTestMutex;
pthread_cond_t lTestCondVar;
pthread_condattr_t lCondAttr;
pthread_mutexattr_t lTestMutexAttr;
pthread_mutexattr_t defaultattr;
pthread_mutexattr_t errorcheckattr;
pthread_mutexattr_t recursiveattr;
pthread_mutexattr_init(&defaultattr);
pthread_mutexattr_init(&errorcheckattr);
pthread_mutexattr_init(&recursiveattr);
pthread_mutexattr_settype(&errorcheckattr,PTHREAD_MUTEX_ERRORCHECK);
pthread_mutexattr_settype(&recursiveattr,PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_t l_staticmutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t l_errorcheckmutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
pthread_mutex_t l_recursivemutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
pthread_cond_t l_staticcondvar = PTHREAD_COND_INITIALIZER;
CommonData lCommonData;
lCommonData.iStaticMutex = &l_staticmutex;
lCommonData.iErrorCheckMutex = &l_errorcheckmutex;
lCommonData.iRecursiveMutex = &l_recursivemutex;
lCommonData.iStaticCondVar = &l_staticcondvar;
retval = sem_init(&lSignalSemaphore,0,0);
if(retval != 0)
{
return retval;
}
retval = sem_init(&lSuspendSemaphore,0,0);
if(retval != 0)
{
return retval;
}
lThreadData.iSignalSemaphore = &lSignalSemaphore;
lThreadData.iSuspendSemaphore = &lSuspendSemaphore;
lThreadData.iTestSemaphore = &lTestSemaphore;
lThreadData.iTestMutex = &lTestMutex;
lThreadData.iTestMutexAttr = &lTestMutexAttr;
lThreadData.iTestCondVar = &lTestCondVar;
lThreadData.iDefaultAttr = &defaultattr;
lThreadData.iErrorcheckAttr = &errorcheckattr;
lThreadData.iRecursiveAttr = &recursiveattr;
lThreadData.iCondAttr = &lCondAttr;
for (int loop = 0; loop < EThreadMain; loop++)
{
g_spinFlag[loop] = true;
}
lThreadData.iSuspending = false;
lThreadData.iSpinCounter = 0;
lThreadData.iCurrentCommand = -1;
lThreadData.iSelf = EThreadMain;
lThreadData.iValue = 0;
lThreadData.iRetValue = 0;
lThreadData.ierrno = 0;
lThreadData.iExpectederrno = 0;
lThreadData.iTimes = 0;
lThreadData.iStopped = false;
lThreadData.iCommonData = &lCommonData;
retval = SemInit(&lThreadData);
fp=func2;
retval = ThreadCreate(&lThreadData, (void*) EThread1);
fp=func4;
retval = ThreadCreate(&lThreadData, (void*) EThread2);
retval = SemPost(&lThreadData);
retval = ThreadDestroy(&lThreadData, (void*) EThread1);
retval = ThreadDestroy(&lThreadData, (void*) EThread2);
retval = SemDestroy(&lThreadData);
StopThread(&lThreadData);
err = pthread_cond_destroy(&l_staticcondvar);
if(err != EINVAL)
{
errsum += err;
}
err = pthread_mutex_destroy(&l_recursivemutex);
if(err != EINVAL)
//.........这里部分代码省略.........
示例12: __taskInit
static STATUS __taskInit(struct wind_task *task,
struct WIND_TCB *tcb, const char *name,
int prio, int flags, FUNCPTR entry, int stacksize)
{
struct threadobj_init_data idata;
pthread_mutexattr_t mattr;
struct sched_param param;
pthread_attr_t thattr;
int ret, cprio;
ret = check_task_priority(prio, &cprio);
if (ret) {
errno = ret;
return ERROR;
}
if (name == NULL || *name == '\0')
sprintf(task->name, "t%lu", ++anon_tids);
else {
strncpy(task->name, name, sizeof(task->name));
task->name[sizeof(task->name) - 1] = '\0';
}
__RT(pthread_mutexattr_init(&mattr));
__RT(pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE));
__RT(pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT));
__RT(pthread_mutexattr_setpshared(&mattr, mutex_scope_attribute));
__RT(pthread_mutex_init(&task->safelock, &mattr));
__RT(pthread_mutexattr_destroy(&mattr));
task->tcb = tcb;
tcb->opaque = task;
/*
* CAUTION: tcb->status in only modified by the owner task
* (see suspend/resume hooks), or when such task is guaranteed
* not to be running, e.g. in taskActivate(). So we do NOT
* take any lock specifically for updating it. However, we
* know that a memory barrier will be issued shortly after
* such updates because of other locking being in effect, so
* we don't explicitely have to provide for it.
*/
tcb->status = WIND_SUSPEND;
tcb->safeCnt = 0;
tcb->flags = flags;
tcb->entry = entry;
pthread_attr_init(&thattr);
if (stacksize == 0)
stacksize = PTHREAD_STACK_MIN * 4;
else if (stacksize < PTHREAD_STACK_MIN)
stacksize = PTHREAD_STACK_MIN;
memset(¶m, 0, sizeof(param));
param.sched_priority = cprio;
pthread_attr_setinheritsched(&thattr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&thattr, SCHED_RT);
pthread_attr_setschedparam(&thattr, ¶m);
pthread_attr_setstacksize(&thattr, stacksize);
pthread_attr_setscope(&thattr, thread_scope_attribute);
pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);
idata.magic = task_magic;
idata.wait_hook = task_wait_hook;
idata.suspend_hook = task_suspend_hook;
idata.finalizer = task_finalizer;
idata.priority = cprio;
threadobj_init(&task->thobj, &idata);
ret = __bt(cluster_addobj(&wind_task_table, task->name, &task->cobj));
if (ret) {
warning("duplicate task name: %s", task->name);
threadobj_destroy(&task->thobj);
__RT(pthread_mutex_destroy(&task->safelock));
errno = S_objLib_OBJ_ID_ERROR;
return ERROR;
}
registry_init_file(&task->fsobj, ®istry_ops);
ret = __bt(-__RT(pthread_create(&task->thobj.tid, &thattr,
&task_trampoline, task)));
pthread_attr_destroy(&thattr);
if (ret) {
registry_destroy_file(&task->fsobj);
cluster_delobj(&wind_task_table, &task->cobj);
threadobj_destroy(&task->thobj);
__RT(pthread_mutex_destroy(&task->safelock));
errno = ret == -EAGAIN ? S_memLib_NOT_ENOUGH_MEMORY : -ret;
return ERROR;
}
return OK;
}
示例13: init
void init() {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
assert(pthread_mutex_init(&native_, &attr) == 0);
}
示例14: log_init
int log_init()
{
time_t t;
struct tm tm;
int result = 0;
int log_thread_lock_init = 0, disk_thread_lock_init = 0, disk_thread_cond_init = 0;
pthread_mutexattr_t mtxAttr;
pthread_t disk_pthread_id;
pthread_attr_t thread_attr;
if (logger_init_flag) {
fprintf(stderr, "logger module has already been init!!\n");
return 1;
}
do {
pContext = (LogContext *)malloc(sizeof(LogContext));
if (pContext == NULL) {
fprintf(stderr, "malloc %lu bytes fail, errno: %d, error info: %s", sizeof(LogContext), errno, STRERROR(errno));
return (errno != 0) ? errno : ENOMEM;
}
log_level = LOG_INFO;
pContext->log_fd = STDERR_FILENO;
pContext->log_base_path = NULL;
pContext->log_filename_prefix = NULL;
pContext->log_to_cache = false;
pContext->logrotate_cycle = ROTATE_CYCLE_DAY;
/* 初始化日志buff */
pContext->log_buff = (char *)malloc(LOG_BUFF_SIZE);
if (pContext->log_buff == NULL) {
fprintf(stderr, "malloc %d bytes fail, errno: %d, error info: %s", LOG_BUFF_SIZE, errno, STRERROR(errno));
result = (errno != 0) ? errno : ENOMEM;
break;
}
pContext->pcurrent_buff = pContext->log_buff;
/* 设置锁为纠错锁, 同一线程不能重复加锁, 加上的锁只能由本线程解锁. 先等待锁的线程先获得锁 */
pthread_mutexattr_init(&mtxAttr);
pthread_mutexattr_settype(&mtxAttr, PTHREAD_MUTEX_ERRORCHECK_NP);
if ((result = pthread_mutex_init(&(pContext->log_thread_lock), &mtxAttr)) != 0) {
fprintf(stderr, "call pthread_mutex_init fail, errno: %d, error info: %s", result, STRERROR(result));
break;
}
pthread_mutexattr_destroy(&mtxAttr);
log_thread_lock_init = 1;
/* 初始化写磁盘线程上下文 */
if ((result = pthread_mutex_init(&(pContext->disk_thread_ctx.disk_thread_lock), NULL)) != 0) {
fprintf(stderr, "call pthread_mutex_init fail, errno: %d, error info: %s", result, STRERROR(result));
break;
}
disk_thread_lock_init = 1;
if ((result = pthread_cond_init(&(pContext->disk_thread_ctx.disk_thread_cond), NULL)) != 0) {
fprintf(stderr, "call pthread_cond_init fail, errno: %d, error info: %s", result, STRERROR(result));
break;
}
disk_thread_cond_init = 1;
pContext->disk_thread_ctx.disk_thread_stop = false;
pContext->disk_thread_ctx.blocking_queue = new std::deque<std::string>;
/* 启动磁盘线程 */
pthread_attr_init(&thread_attr);
pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
if ((result = pthread_create(&disk_pthread_id, &thread_attr, disk_write_thread, NULL)) != 0) {
fprintf(stderr, "call pthread_create fail, errno: %d, error info: %s", result, STRERROR(result));
break;
}
pContext->disk_thread_ctx.disk_pthread_id = disk_pthread_id;
pthread_attr_destroy(&thread_attr);
/* 等待磁盘线程运行成功 */
pthread_mutex_init(&thread_init_lock, NULL);
pthread_cond_init(&thread_init_cond, NULL);
pthread_mutex_lock(&thread_init_lock);
while (!thread_init_flag) {
pthread_cond_wait(&thread_init_cond, &thread_init_lock);
}
pthread_mutex_unlock(&thread_init_lock);
/* 记录当前时间 */
t = time(NULL);
localtime_r(&t, &tm);
switch (pContext->logrotate_cycle) {
//.........这里部分代码省略.........
示例15: pthread_mutexattr_init
void eMP3Decoder::init_eMP3Decoder(const char* filename)
{
state=stateInit;
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&lock, &attr);
pthread_mutexattr_destroy(&attr);
http=0;
seekbusy=0;
// filename="http://205.188.209.193:80/stream/1003";
// filename="http://sik1.oulu.fi:8002/";
// filename="http://64.236.34.141:80/stream/1022";
// filename="http://ios.h07.org:8006/";
// filename="http://10.0.0.112/join.mp3";
if (strstr(filename, "://")) // assume streaming
{
if (!strncmp(filename, "file://", 7))
filename+=7;
else
{
http=eHTTPConnection::doRequest(filename, this, &error);
if (!http)
{
streamingDone(error);
} else
{
CONNECT(http->transferDone, eMP3Decoder::streamingDone);
CONNECT(http->createDataSource, eMP3Decoder::createStreamSink);
http->local_header["User-Agent"]="enigma-mp3/1.0.0";
http->local_header["Icy-MetaData"]="1"; // enable ICY metadata
http->start();
http_status=_("Connecting...");
filelength=-1;
handler->messages.send(eServiceHandlerMP3::eMP3DecoderMessage(eServiceHandlerMP3::eMP3DecoderMessage::status));
}
filename=0;
}
}
if (filename) // not streaming
{
sourcefd=::open(filename, O_RDONLY|O_LARGEFILE);
if (sourcefd<0)
{
error=errno;
eDebug("error opening %s", filename);
state=stateError;
} else
{
filelength=lseek64(sourcefd, 0, SEEK_END);
lseek(sourcefd, 0, SEEK_SET);
}
} else
sourcefd=-1;
length=-1;
Decoder::Flush();
if (type != codecMPG)
{
divisor=1;
pcmsettings.reconfigure=1;
dspfd[1]=-1;
dspfd[0]=::open("/dev/dsp", O_WRONLY|O_NONBLOCK);
if (dspfd[0]<0)
{
eDebug("output failed! (%m)");
error=errno;
state=stateError;
}
if (dspfd[0] >= 0)
{
outputsn[0]=new eSocketNotifier(this, dspfd[0], eSocketNotifier::Write, 0);
CONNECT(outputsn[0]->activated, eMP3Decoder::outputReady);
} else
outputsn[0]=0;
outputsn[1]=0;
} else
{
divisor=1024;
Decoder::parms.vpid=0x1FFF;
Decoder::parms.apid=0x1FFF;
Decoder::parms.pcrpid=-1;
Decoder::parms.audio_type=DECODE_AUDIO_MPEG;
Decoder::Set();
dspfd[0]=::open("/dev/video0", O_WRONLY);
dspfd[1]=::open("/dev/dsp", O_WRONLY);
if ((dspfd[0]<0) || (dspfd[1]<0))
{
if (dspfd[0]>=0)
//.........这里部分代码省略.........