本文整理汇总了C++中pthread_attr_getstacksize函数的典型用法代码示例。如果您正苦于以下问题:C++ pthread_attr_getstacksize函数的具体用法?C++ pthread_attr_getstacksize怎么用?C++ pthread_attr_getstacksize使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pthread_attr_getstacksize函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: parsecfg
static int parsecfg(xmlDocPtr doc, xmlNodePtr node, struct state *state, unsigned int *schedrr)
{
xmlNodePtr audio = NULL;
xmlNodePtr ptt = NULL;
const char *par[MAXPAR];
struct modemchannel *chan;
pthread_attr_t rxattr;
size_t stacksize;
unsigned int samplerate = 5000, mode;
for (; node; node = node->next) {
if (!node->name)
logprintf(MLOG_FATAL, "Node has no name\n");
if (!strcmp(node->name, "audio")) {
audio = node;
continue;
}
if (!strcmp(node->name, "ptt")) {
ptt = node;
continue;
}
if (!strcmp(node->name, "chaccess")) {
getparam(doc, node, chaccparams, par);
if (par[0])
state->chacc.txdelay = strtoul(par[0], NULL, 0);
if (par[1])
state->chacc.slottime = strtoul(par[1], NULL, 0);
if (par[2])
state->chacc.ppersist = strtoul(par[2], NULL, 0);
if (par[3])
state->chacc.fullduplex = !!strtoul(par[3], NULL, 0);
if (par[4])
state->chacc.txtail = strtoul(par[4], NULL, 0);
continue;
}
if (!strcmp(node->name, "channel")) {
if (node->childs)
parsechannel(doc, node->childs, state, &samplerate);
continue;
}
logprintf(MLOG_ERROR, "unknown node \"%s\"\n", node->name);
}
/* find audio mode */
mode = 0;
for (chan = state->channels; chan; chan = chan->next) {
if (chan->demod && chan->demod->demodulate)
mode |= IO_RDONLY;
if (chan->mod && chan->mod->modulate)
mode |= IO_WRONLY;
}
if (!state->channels || !mode) {
logprintf(MLOG_ERROR, "no channels configured\n");
return -1;
}
/* open PTT */
getparam(doc, ptt, pttparams, par);
if (pttinit(&state->ptt, par))
logprintf(MLOG_ERROR, "cannot start PTT output\n");
/* open audio */
getparam(doc, audio, ioparam_type, par);
if (par[0] && !strcmp(par[0], ioparam_type[0].u.c.combostr[1])) {
getparam(doc, audio, ioparams_filein, par);
state->audioio = ioopen_filein(&samplerate, IO_RDONLY, par);
if (schedrr)
*schedrr = 0;
} else if (par[0] && !strcmp(par[0], ioparam_type[0].u.c.combostr[2])) {
getparam(doc, audio, ioparams_sim, par);
state->audioio = ioopen_sim(&samplerate, IO_RDWR, par);
if (schedrr)
*schedrr = 0;
} else {
getparam(doc, audio, ioparams_soundcard, par);
state->audioio = ioopen_soundcard(&samplerate, mode, par);
}
if (!state->audioio)
logprintf(MLOG_FATAL, "cannot start audio\n");
for (chan = state->channels; chan; chan = chan->next) {
if (chan->demod) {
chan->demod->init(chan->demodstate, samplerate, &chan->rxbitrate);
if (pthread_attr_init(&rxattr))
logerr(MLOG_FATAL, "pthread_attr_init");
/* needed on FreeBSD, according to [email protected] */
if (pthread_attr_getstacksize(&rxattr, &stacksize))
logerr(MLOG_ERROR, "pthread_attr_getstacksize");
else if (stacksize < 256*1024)
if (pthread_attr_setstacksize(&rxattr, 256*1024))
logerr(MLOG_ERROR, "pthread_attr_setstacksize");
#ifdef HAVE_SCHED_H
if (schedrr && *schedrr) {
struct sched_param schp;
memset(&schp, 0, sizeof(schp));
schp.sched_priority = sched_get_priority_min(SCHED_RR)+1;
if (pthread_attr_setschedpolicy(&rxattr, SCHED_RR))
logerr(MLOG_ERROR, "pthread_attr_setschedpolicy");
if (pthread_attr_setschedparam(&rxattr, &schp))
logerr(MLOG_ERROR, "pthread_attr_setschedparam");
}
#endif /* HAVE_SCHED_H */
if (pthread_create(&chan->rxthread, &rxattr, demodthread, chan))
logerr(MLOG_FATAL, "pthread_create");
//.........这里部分代码省略.........
示例2: TIMM_OSAL_CreateTask
TIMM_OSAL_ERRORTYPE TIMM_OSAL_CreateTask (TIMM_OSAL_PTR *pTask,
TIMM_OSAL_TaskProc pFunc,
TIMM_OSAL_U32 uArgc,
TIMM_OSAL_PTR pArgv,
TIMM_OSAL_U32 uStackSize,
TIMM_OSAL_U32 uPriority,
TIMM_OSAL_S8 *pName)
{
TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_UNKNOWN;
TIMM_OSAL_TASK *pHandle = TIMM_OSAL_NULL;
struct sched_param sched;
size_t stackSize;
*pTask = TIMM_OSAL_NULL;
unsigned int temp = (unsigned int)pFunc;
/*Task structure allocation*/
pHandle = (TIMM_OSAL_TASK *) TIMM_OSAL_Malloc(sizeof(TIMM_OSAL_TASK), 0, 0, 0);
if(pHandle == TIMM_OSAL_NULL) {
bReturnStatus = TIMM_OSAL_ERR_ALLOC;
goto EXIT;
}
/* Initial cleaning of the task structure */
TIMM_OSAL_Memset((TIMM_OSAL_PTR)pHandle, 0, sizeof(TIMM_OSAL_TASK));
/*Arguments for task*/
pHandle->uArgc = uArgc;
pHandle->pArgv = pArgv;
pHandle->isCreated = TIMM_OSAL_FALSE;
if(SUCCESS != pthread_attr_init(&pHandle->ThreadAttr)){
/*TIMM_OSAL_Error("Task Init Attr Init failed!");*/
goto EXIT;
}
/* Updation of the priority and the stack size*/
if(SUCCESS != pthread_attr_getschedparam(&pHandle->ThreadAttr, &sched)){
/*TIMM_OSAL_Error("Task Init Get Sched Params failed!");*/
goto EXIT;
}
sched.sched_priority = uPriority; /* relative to the default priority */
if(SUCCESS != pthread_attr_setschedparam(&pHandle->ThreadAttr, &sched)){
/*TIMM_OSAL_Error("Task Init Set Sched Paramsfailed!");*/
goto EXIT;
}
/*First get the default stack size*/
if(SUCCESS != pthread_attr_getstacksize(&pHandle->ThreadAttr, &stackSize)){
/*TIMM_OSAL_Error("Task Init Set Stack Size failed!");*/
goto EXIT;
}
/*Check if requested stack size is larger than the current default stack size*/
if(uStackSize > stackSize) {
stackSize = uStackSize;
if(SUCCESS != pthread_attr_setstacksize(&pHandle->ThreadAttr, stackSize)){
/*TIMM_OSAL_Error("Task Init Set Stack Size failed!");*/
goto EXIT;
}
}
if (SUCCESS != pthread_create(&pHandle->threadID, &pHandle->ThreadAttr, pFunc, pArgv)) {
/*TIMM_OSAL_Error ("Create_Task failed !");*/
goto EXIT;
}
/* Task was successfully created */
pHandle->isCreated = TIMM_OSAL_TRUE;
*pTask = (TIMM_OSAL_PTR )pHandle;
bReturnStatus = TIMM_OSAL_ERR_NONE;
/**pTask = (TIMM_OSAL_PTR *)pHandle;*/
EXIT:
/* if((TIMM_OSAL_ERR_NONE != bReturnStatus) && (TIMM_OSAL_NULL != pHandle)) {
TIMM_OSAL_Free (pHandle->stackPtr);*/
if((TIMM_OSAL_ERR_NONE != bReturnStatus)) {
TIMM_OSAL_Free(pHandle);
}
return bReturnStatus;
}
示例3: chpl_thread_init
void chpl_thread_init(void(*threadBeginFn)(void*),
void(*threadEndFn)(void)) {
//
// This threading layer does not have any inherent limit on the number
// of threads. Its limit is the lesser of any limits imposed by the
// comm layer and the user.
//
{
uint32_t lim;
if ((lim = chpl_task_getenvNumThreadsPerLocale()) > 0)
maxThreads = lim;
else if ((lim = chpl_comm_getMaxThreads()) > 0)
maxThreads = lim;
}
//
// Count the main thread on locale 0 as already existing, since it
// is (or soon will be) running the main program.
//
if (chpl_nodeID == 0)
numThreads = 1;
//
// If a value was specified for the call stack size config const, use
// that (rounded up to a whole number of pages) to set the system and
// pthread stack limits.
//
if (pthread_attr_init(&thread_attributes) != 0)
chpl_internal_error("pthread_attr_init() failed");
//
// If a value was specified for the call stack size, use that (rounded
// up to a whole number of pages) to set the system and pthread stack
// limits. This will in turn limit the stack for any task hosted by
// either the main process or a pthread.
//
{
size_t css;
size_t pagesize = (size_t) sysconf(_SC_PAGESIZE);
struct rlimit rlim;
if ((css = chpl_task_getEnvCallStackSize()) == 0)
css = chpl_task_getDefaultCallStackSize();
assert(css > 0);
css = (css + pagesize - 1) & ~(pagesize - 1);
if (getrlimit(RLIMIT_STACK, &rlim) != 0)
chpl_internal_error("getrlimit() failed");
if (rlim.rlim_max != RLIM_INFINITY && css > rlim.rlim_max) {
char warning[128];
sprintf(warning, "call stack size capped at %lu\n",
(unsigned long)rlim.rlim_max);
chpl_warning(warning, 0, 0);
css = rlim.rlim_max;
}
rlim.rlim_cur = css;
#ifndef __CYGWIN__
//
// Cygwin can't do setrlimit(RLIMIT_STACK).
//
if (setrlimit(RLIMIT_STACK, &rlim) != 0)
chpl_internal_error("setrlimit() failed");
#endif
if (pthread_attr_setstacksize(&thread_attributes, css) != 0)
chpl_internal_error("pthread_attr_setstacksize() failed");
}
if (pthread_attr_getstacksize(&thread_attributes, &threadCallStackSize) != 0)
chpl_internal_error("pthread_attr_getstacksize() failed");
saved_threadBeginFn = threadBeginFn;
saved_threadEndFn = threadEndFn;
CHPL_TLS_INIT(chpl_thread_id);
CHPL_TLS_SET(chpl_thread_id, (intptr_t) --curr_thread_id);
CHPL_TLS_INIT(chpl_thread_data);
pthread_mutex_init(&thread_info_lock, NULL);
pthread_mutex_init(&numThreadsLock, NULL);
//
// This is something of a hack, but it makes us a bit more resilient
// if we're out of memory or near to it at shutdown time. Launch,
// cancel, and join with an initial pthread, forcing initialization
// needed by any of those activities. (In particular we have found
// that cancellation needs to dlopen(3) a shared object, which fails
// if we are out of memory. Doing it now means that shared object is
// already available when we need it later.)
//
{
pthread_t initial_pthread;
if (!pthread_create(&initial_pthread, NULL, initial_pthread_func, NULL)) {
//.........这里部分代码省略.........
示例4: main
int main(int argn, char *argc[])
{
//Program parameters : argc[1] : HostName or Host IP
// argc[2] : Server Program Number
// argc[3] : Maximal number of threads
// argc[4] : Number of calls per thread
// other arguments depend on test case
//run_mode can switch into stand alone program or program launch by shell script
//1 : stand alone, debug mode, more screen information
//0 : launch by shell script as test case, only one printf -> result status
run_mode = 0;
int test_status = 1; //Default test result set to FAILED
int i, rthcreate;
long j;
int threadNb = atoi((char *)argc[3]);
int curThd = 1;
//Thread declarations
pthread_t *pThreadArray;
void *ret = NULL;
pthread_attr_t thread_attr;
int ssz = 0;
//Time measurement declarations
struct timeval tv1, tv2;
struct timezone tz;
long long diff;
double rslt;
//Program initialization
progNum = atoi((char *)argc[2]);
callNb = atoi((char *)argc[4]);
hostname = (char *)argc[1];
//Initialization
maxThd = maxThd << (threadNb - 1); //Set the maximum threads number
pthread_attr_init(&thread_attr);
if (run_mode == 1) {
pthread_attr_getstacksize(&thread_attr, (size_t *) & ssz); //For debug purpose, get default thread stack size
fprintf(stderr, "Server #%d\n", progNum);
fprintf(stderr, "Calls per thread : %d\n", callNb);
fprintf(stderr, "Instances : %d\n", threadNb);
fprintf(stderr, "Max threads to create : %d\n", maxThd);
fprintf(stderr, "Standard thread stack size in bytes %d\n",
ssz);
}
pthread_attr_setstacksize(&thread_attr, 40000); //Set thread stack size to 40 KB
//Init results table
thread_time_result = (double *)malloc((threadNb) * sizeof(double));
memset(&thread_time_result[0], (double)0, (threadNb) * sizeof(double));
//Create all threads
//Run all threads
pThreadArray = (pthread_t *) malloc(maxThd * sizeof(pthread_t));
for (i = 0; i < threadNb; i++) {
if (run_mode)
fprintf(stderr, "Threads for pass %d : %d\n", i,
curThd);
gettimeofday(&tv1, &tz);
for (j = 0; j < curThd; j++) {
//Create thread using defined parameters (stack size = 40 KB)
if (pthread_create
(&pThreadArray[j], &thread_attr, my_thread_process,
(void *)j) != 0) {
fprintf(stderr,
"pthread_create error for thread %d\n",
j);
printf("1\n");
exit(1);
}
}
//Clean threads
for (j = 0; j < curThd; j++) {
if ((pthread_t *) pThreadArray[j] != NULL) {
(void)pthread_join(pThreadArray[j], &ret);
} else {
fprintf(stderr, "pThread Join Err : %d\n", j);
}
}
gettimeofday(&tv2, &tz);
//Calculate and store delay to table results
diff =
(tv2.tv_sec - tv1.tv_sec) * 1000000L + (tv2.tv_usec -
tv1.tv_usec);
rslt = (double)diff / 1000;
thread_time_result[i] = rslt;
curThd = curThd * 2;
}
//.........这里部分代码省略.........
示例5: get_stack
/*
* Get the initial address and size of current thread's stack
*/
static int
get_stack(void **addr, size_t *size)
{
#define CHECK_ERR(expr) \
{int err = (expr); if (err) return err;}
#ifdef HAVE_PTHREAD_GETATTR_NP /* Linux */
pthread_attr_t attr;
size_t guard = 0;
STACK_GROW_DIR_DETECTION;
CHECK_ERR(pthread_getattr_np(pthread_self(), &attr));
# ifdef HAVE_PTHREAD_ATTR_GETSTACK
CHECK_ERR(pthread_attr_getstack(&attr, addr, size));
STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
# else
CHECK_ERR(pthread_attr_getstackaddr(&attr, addr));
CHECK_ERR(pthread_attr_getstacksize(&attr, size));
# endif
CHECK_ERR(pthread_attr_getguardsize(&attr, &guard));
*size -= guard;
pthread_attr_destroy(&attr);
#elif defined HAVE_PTHREAD_ATTR_GET_NP /* FreeBSD, DragonFly BSD, NetBSD */
pthread_attr_t attr;
CHECK_ERR(pthread_attr_init(&attr));
CHECK_ERR(pthread_attr_get_np(pthread_self(), &attr));
# ifdef HAVE_PTHREAD_ATTR_GETSTACK
CHECK_ERR(pthread_attr_getstack(&attr, addr, size));
# else
CHECK_ERR(pthread_attr_getstackaddr(&attr, addr));
CHECK_ERR(pthread_attr_getstacksize(&attr, size));
# endif
STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
pthread_attr_destroy(&attr);
#elif (defined HAVE_PTHREAD_GET_STACKADDR_NP && defined HAVE_PTHREAD_GET_STACKSIZE_NP) /* MacOS X */
pthread_t th = pthread_self();
*addr = pthread_get_stackaddr_np(th);
*size = pthread_get_stacksize_np(th);
#elif defined HAVE_THR_STKSEGMENT || defined HAVE_PTHREAD_STACKSEG_NP
stack_t stk;
# if defined HAVE_THR_STKSEGMENT /* Solaris */
CHECK_ERR(thr_stksegment(&stk));
# else /* OpenBSD */
CHECK_ERR(pthread_stackseg_np(pthread_self(), &stk));
# endif
*addr = stk.ss_sp;
*size = stk.ss_size;
#elif defined HAVE_PTHREAD_GETTHRDS_NP /* AIX */
pthread_t th = pthread_self();
struct __pthrdsinfo thinfo;
char reg[256];
int regsiz=sizeof(reg);
CHECK_ERR(pthread_getthrds_np(&th, PTHRDSINFO_QUERY_ALL,
&thinfo, sizeof(thinfo),
®, ®siz));
*addr = thinfo.__pi_stackaddr;
*size = thinfo.__pi_stacksize;
STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
#else
#error STACKADDR_AVAILABLE is defined but not implemented.
#endif
return 0;
#undef CHECK_ERR
}
示例6: main
//.........这里部分代码省略.........
sigaddset(&sa.sa_mask, SIGTERM);
sa.sa_handler = sig_handler_exit;
sa.sa_flags = 0;
if(sigaction(SIGHUP, &sa, NULL) == -1) {
error("Failed to change signal handler for SIGHUP");
}
if(sigaction(SIGINT, &sa, NULL) == -1) {
error("Failed to change signal handler for SIGINT");
}
if(sigaction(SIGTERM, &sa, NULL) == -1) {
error("Failed to change signal handler for SIGTERM");
}
// save database on SIGUSR1
sa.sa_handler = sig_handler_save;
if(sigaction(SIGUSR1, &sa, NULL) == -1) {
error("Failed to change signal handler for SIGUSR1");
}
// Ignore SIGPIPE completely.
// INFO: If we add signals here we have to unblock them
// at popen.c when running a external plugin.
sa.sa_handler = SIG_IGN;
if(sigaction(SIGPIPE, &sa, NULL) == -1) {
error("Failed to change signal handler for SIGPIPE");
}
// --------------------------------------------------------------------
i = pthread_attr_init(&attr);
if(i != 0)
fatal("pthread_attr_init() failed with code %d.", i);
i = pthread_attr_getstacksize(&attr, &stacksize);
if(i != 0)
fatal("pthread_attr_getstacksize() failed with code %d.", i);
else
debug(D_OPTIONS, "initial pthread stack size is %zu bytes", stacksize);
wanted_stacksize = config_get_number("global", "pthread stack size", stacksize);
// --------------------------------------------------------------------
for (i = 0; static_threads[i].name != NULL ; i++) {
struct netdata_static_thread *st = &static_threads[i];
if(st->config_name) st->enabled = config_get_boolean(st->config_section, st->config_name, st->enabled);
if(st->enabled && st->init_routine) st->init_routine();
}
// --------------------------------------------------------------------
// get the user we should run
// IMPORTANT: this is required before web_files_uid()
user = config_get("global", "run as user" , (getuid() == 0)?NETDATA_USER:"");
// IMPORTANT: these have to run once, while single threaded
web_files_uid(); // IMPORTANT: web_files_uid() before web_files_gid()
web_files_gid();
// --------------------------------------------------------------------
create_listen_sockets();
}
// never become a problem
示例7: GetActualStackSizeFn
static void* GetActualStackSizeFn(void* arg) {
pthread_attr_t attributes;
pthread_getattr_np(pthread_self(), &attributes);
pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
return NULL;
}
示例8: ydb_storage_syncquery_init
pthread_t ydb_storage_syncquery_init(
SpxLogDelegate *log,
u32_t timeout,
struct ydb_storage_configurtion *config,\
err_t *err){/*{{{*/
struct spx_job_context_transport arg;
SpxZero(arg);
arg.timeout = timeout;
arg.nio_reader = spx_nio_reader;
arg.nio_writer = spx_nio_writer;
arg.log = log;
arg.reader_body_process = ydb_storage_syncquery_nio_body_reader;
arg.writer_body_process = ydb_storage_syncquery_nio_body_writer;
arg.config = config;
sync_timer = spx_alloc_alone(sizeof(*sync_timer),err);
if(NULL == sync_timer){
SpxLog2(log,SpxLogError,*err,"alloc sync timer is fail.");
goto r1;
}
sloop = ev_loop_new(0);
if(NULL == sloop){
*err = errno;
SpxLog2(log,SpxLogError,*err,\
"new event loop for sync is fail.");
goto r1;
}
if(!ydb_storage_get_remote(config,&arg)){
goto r1;
}
pthread_t tid = 0;
pthread_attr_t attr;
pthread_attr_init(&attr);
size_t ostack_size = 0;
pthread_attr_getstacksize(&attr, &ostack_size);
do{
if (ostack_size != config->stacksize
&& (0 != (*err = pthread_attr_setstacksize(&attr,config->stacksize)))){
pthread_attr_destroy(&attr);
SpxLog2(log,SpxLogError,*err,\
"set thread stack size is fail.");
goto r1;
}
if (0 !=(*err = pthread_create(&(tid), &attr,
ydb_storage_syncquery_heartbeat,
NULL))){
pthread_attr_destroy(&attr);
SpxLog2(log,SpxLogError,*err,\
"create heartbeat thread is fail.");
goto r1;
}
}while(false);
pthread_attr_destroy(&attr);
return tid;
r1:
if(NULL != sync_timer){
SpxFree(sync_timer);
}
return 0;
}/*}}}*/
示例9: direct_thread_init
DirectResult
direct_thread_init( DirectThread *thread )
{
pthread_attr_t attr;
struct sched_param param;
int policy;
int priority;
direct_once( &thread_init_once, init_once );
/* Initialize scheduling and other parameters. */
pthread_attr_init( &attr );
#ifdef PTHREAD_EXPLICIT_SCHED
pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
#endif
/* Select scheduler. */
switch (direct_config->thread_scheduler) {
case DCTS_FIFO:
policy = SCHED_FIFO;
break;
case DCTS_RR:
policy = SCHED_RR;
break;
default:
policy = SCHED_OTHER;
break;
}
if (pthread_attr_setschedpolicy( &attr, policy ))
D_PERROR( "Direct/Thread: Could not set scheduling policy to %s!\n", direct_thread_policy_name(policy) );
/* Read (back) value. */
pthread_attr_getschedpolicy( &attr, &policy );
/* Select priority. */
switch (thread->type) {
case DTT_CLEANUP:
case DTT_INPUT:
case DTT_OUTPUT:
case DTT_MESSAGING:
case DTT_CRITICAL:
priority = thread->type * direct_config->thread_priority_scale / 100;
break;
default:
priority = direct_config->thread_priority;
break;
}
D_DEBUG_AT( Direct_ThreadInit, " -> %s (%d) [%d;%d]\n", direct_thread_policy_name(policy), priority,
sched_get_priority_min( policy ), sched_get_priority_max( policy ) );
if (priority < sched_get_priority_min( policy ))
priority = sched_get_priority_min( policy );
if (priority > sched_get_priority_max( policy ))
priority = sched_get_priority_max( policy );
param.sched_priority = priority;
if (pthread_attr_setschedparam( &attr, ¶m ))
D_PERROR( "Direct/Thread: Could not set scheduling priority to %d!\n", priority );
/* Select stack size? */
if (direct_config->thread_stack_size > 0) {
if (pthread_attr_setstacksize( &attr, direct_config->thread_stack_size ))
D_PERROR( "Direct/Thread: Could not set stack size to %d!\n", direct_config->thread_stack_size );
}
if (pthread_create( &thread->handle.thread, &attr, direct_thread_main, thread ))
return errno2result( errno );
pthread_attr_destroy( &attr );
/* Read (back) value. */
pthread_getattr_np( thread->handle.thread, &attr );
pthread_attr_getstacksize( &attr, &thread->stack_size );
pthread_attr_getschedparam( &attr, ¶m );
thread->priority = param.sched_priority;
pthread_attr_destroy( &attr );
return DR_OK;
}
示例10: printf
void *my_thread(void *arg){
int retval = 0;
pthread_attr_t attr;
struct sched_param param;
size_t stacksize;
int detachstate;
int scope;
int inherit;
int policy;
if(pthread_attr_init (&attr) == 0){
if(pthread_attr_getstacksize(&attr, &stacksize) == 0){
printf("StackSize :%d\n", stacksize);
}
if(pthread_attr_getdetachstate (&attr, &detachstate) == 0){
if(detachstate == PTHREAD_CREATE_JOINABLE){
printf("DetachState : PTHREAD_CREATE_JOINABLE\n");
}else if(detachstate == PTHREAD_CREATE_DETACHED){
printf("DetachState:PTHREAD_CREATE_DETACHED\n");
}
}
if(pthread_attr_getscope (&attr, &scope) == 0){
if(scope == PTHREAD_SCOPE_PROCESS){
printf("Scope : PTHREAD_SCOPE_PROCESS\n");
}else if(scope == PTHREAD_SCOPE_SYSTEM){
printf("Scope : PTHREAD_SCOPE_SYSTEM\n");
}
}
if(pthread_attr_getinheritsched(&attr, &inherit) == 0){
if(inherit == PTHREAD_INHERIT_SCHED){
printf("InheritSched : PTHREAD_INHERIT_SCHED\n");
}else if(inherit == PTHREAD_EXPLICIT_SCHED){
printf("InheritSched : PTHREAD_EXPLICIT_SCHED\n");
}
}
if(pthread_attr_getschedpolicy (&attr, &policy) == 0){
if(policy == SCHED_FIFO){
printf("SchedPolicy : SCHED_FIFO\n");
}else if(policy == SCHED_RR){
printf("SchedPolicy:SCHED_RR\n");
}else{
printf("SchedPolicy :SCHED_OTHER\n");
}
}
if(pthread_attr_getschedparam (&attr, ¶m) == 0){
printf("schedPriority:%d\n", param.sched_priority);
}
pthread_attr_destroy (&attr);
}
pthread_exit(&retval);
}
示例11: VXItrdThreadCreate
/**
* Create a thread. Note: thread values are not supported on OS/2.
* execution starts on the thread immediately. To pause execution
* use a Mutex between the thread and the thread creator.
*
* @param thread the thread object to be created
* @param thread_function the function for the thread to start execution on
* @param thread_arg the arguments to the thread function
* @return VXItrdResult of operation. Return SUCCESS if thread has been
* created and started.
*
*/
VXITRD_API
VXItrdResult VXItrdThreadCreate(VXItrdThread **thread,
VXItrdThreadStartFunc thread_function,
VXItrdThreadArg thread_arg)
{
if ((thread == NULL) || (thread_function == NULL))
return VXItrd_RESULT_INVALID_ARGUMENT;
*thread = NULL;
// Create the wrapper object
VXItrdThread *result = new VXItrdThread;
if (result == NULL)
return VXItrd_RESULT_OUT_OF_MEMORY;
memset(result, 0, sizeof (VXItrdThread));
result->state = STATE_STARTING;
result->refCount = 1; // 1 for parent
result->thread_function = thread_function;
result->thread_arg = thread_arg;
if (VXItrdMutexCreate(&result->refCountMutex) != VXItrd_RESULT_SUCCESS) {
VXItrdThreadDestroyHandle(&result);
return VXItrd_RESULT_SYSTEM_ERROR;
}
// construct thread attribute
pthread_attr_t thread_attr;
int prc = pthread_attr_init(&thread_attr);
if (prc != 0) {
VXItrdThreadDestroyHandle(&result);
return VXItrd_RESULT_NON_FATAL_ERROR;
}
// configure thread attribute
#ifdef USE_DETACHED_THREADS
prc = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
if (prc != 0) {
pthread_attr_destroy(&thread_attr);
VXItrdThreadDestroyHandle(&result);
return VXItrd_RESULT_NON_FATAL_ERROR;
}
#else
// use joinable threads
// this is default - no work required
#endif
// Instead of using the default 2M as stack size, reduce it to 1M
size_t stacksize = 0;
prc = pthread_attr_getstacksize(&thread_attr, &stacksize);
if (prc == 0) {
prc = pthread_attr_setstacksize(&thread_attr, SIPXCHANGE_STACK_SIZE_128K);
}
// Start the thread using our wrapper function
result->refCount++; // for child
prc = pthread_create(&result->thread, &thread_attr, VXItrdThreadStart,
(VXItrdThreadArg) result);
pthread_attr_destroy(&thread_attr);
if (prc != 0) {
result->refCount--;
VXItrdThreadDestroyHandle(&result);
return VXItrd_RESULT_NON_FATAL_ERROR;
}
*thread = result;
return VXItrd_RESULT_SUCCESS;
}
示例12: TEST_BEGIN
//.........这里部分代码省略.........
status = pthread_attr_getschedpolicy( &attr, NULL );
fatal_directive_check_status_only( status, EINVAL, "NULL schedpolicy" );
puts( "Init - pthread_attr_getschedpolicy - EINVAL (not initialized attr)" );
status = pthread_attr_getschedpolicy( &destroyed_attr, &schedpolicy );
fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );
puts( "Init - pthread_attr_getschedpolicy - SUCCESSFUL" );
status = pthread_attr_getschedpolicy( &attr, &schedpolicy );
posix_service_failed( status, "pthread_attr_getschedpolicy");
printf( "Init - current scheduler policy attribute = %d\n", schedpolicy );
/* exercise get and set stack size */
empty_line();
puts( "Init - pthread_attr_setstacksize - EINVAL (NULL attr)" );
status = pthread_attr_setstacksize( NULL, 0 );
fatal_directive_check_status_only( status, EINVAL, "NULL attr" );
puts( "Init - pthread_attr_setstacksize - EINVAL (not initialized attr)" );
status =
pthread_attr_setstacksize( &destroyed_attr, 0 );
fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );
puts( "Init - pthread_attr_setstacksize - SUCCESSFUL (low stacksize)" );
status = pthread_attr_setstacksize( &attr, 0 );
posix_service_failed( status, "pthread_attr_setstacksize");
puts( "Init - pthread_attr_setstacksize - SUCCESSFUL (high stacksize)" );
status = pthread_attr_setstacksize( &attr, STACK_MINIMUM_SIZE * 2 );
posix_service_failed( status, "");
puts( "Init - pthread_attr_getstacksize - EINVAL (NULL attr)" );
status = pthread_attr_getstacksize( NULL, &stacksize );
fatal_directive_check_status_only( status, EINVAL, "NULL attr" );
puts( "Init - pthread_attr_getstacksize - EINVAL (NULL stacksize)" );
status = pthread_attr_getstacksize( &attr, NULL );
fatal_directive_check_status_only( status, EINVAL, "NULL stacksize" );
puts( "Init - pthread_attr_getstacksize - EINVAL (not initialized attr)" );
status = pthread_attr_getstacksize( &destroyed_attr, &stacksize );
fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );
puts( "Init - pthread_attr_getstacksize - SUCCESSFUL" );
status = pthread_attr_getstacksize( &attr, &stacksize );
posix_service_failed( status, "pthread_attr_getstacksize");
if ( stacksize == (STACK_MINIMUM_SIZE * 2) )
printf( "Init - current stack size attribute is OK\n" );
/* exercise get and set stack address */
empty_line();
puts( "Init - pthread_attr_setstackaddr - EINVAL (NULL attr)" );
status = pthread_attr_setstackaddr( NULL, NULL );
fatal_directive_check_status_only( status, EINVAL, "NULL attr" );
puts( "Init - pthread_attr_setstackaddr - EINVAL (not initialized attr)" );
status = pthread_attr_setstackaddr( &destroyed_attr, NULL );
fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );
puts( "Init - pthread_attr_setstackaddr - SUCCESSFUL" );
status = pthread_attr_setstackaddr( &attr, 0 );
posix_service_failed( status, "");
示例13: TEST
TEST(pthread, pthread_attr_getstack__main_thread) {
// This test is only meaningful for the main thread, so make sure we're running on it!
ASSERT_EQ(getpid(), syscall(__NR_gettid));
// Get the main thread's attributes.
pthread_attr_t attributes;
ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
// Check that we correctly report that the main thread has no guard page.
size_t guard_size;
ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
ASSERT_EQ(0U, guard_size); // The main thread has no guard page.
// Get the stack base and the stack size (both ways).
void* stack_base;
size_t stack_size;
ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
size_t stack_size2;
ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
// The two methods of asking for the stack size should agree.
EXPECT_EQ(stack_size, stack_size2);
#if defined(__BIONIC__)
// What does /proc/self/maps' [stack] line say?
void* maps_stack_hi = NULL;
FILE* fp = fopen("/proc/self/maps", "r");
ASSERT_TRUE(fp != NULL);
char line[BUFSIZ];
while (fgets(line, sizeof(line), fp) != NULL) {
uintptr_t lo, hi;
char name[10];
sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d %10s", &lo, &hi, name);
if (strcmp(name, "[stack]") == 0) {
maps_stack_hi = reinterpret_cast<void*>(hi);
break;
}
}
fclose(fp);
// The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size.
// Remember that the stack grows down (and is mapped in on demand), so the low address of the
// region isn't very interesting.
EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size);
// The stack size should correspond to RLIMIT_STACK.
rlimit rl;
ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
uint64_t original_rlim_cur = rl.rlim_cur;
if (rl.rlim_cur == RLIM_INFINITY) {
rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
}
EXPECT_EQ(rl.rlim_cur, stack_size);
auto guard = make_scope_guard([&rl, original_rlim_cur]() {
rl.rlim_cur = original_rlim_cur;
ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
});
//
// What if RLIMIT_STACK is smaller than the stack's current extent?
//
rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already.
rl.rlim_max = RLIM_INFINITY;
ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
EXPECT_EQ(stack_size, stack_size2);
ASSERT_EQ(1024U, stack_size);
//
// What if RLIMIT_STACK isn't a whole number of pages?
//
rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages.
rl.rlim_max = RLIM_INFINITY;
ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
EXPECT_EQ(stack_size, stack_size2);
ASSERT_EQ(6666U, stack_size);
#endif
}
示例14: thread_create
thread_t *
thread_create (
thread_init_func initial_function,
unsigned long stack_size,
void *initial_argument)
{
thread_t *thr;
int rc;
assert (_main_thread != NULL);
if (stack_size == 0)
stack_size = THREAD_STACK_SIZE;
#if (SIZEOF_VOID_P == 8)
stack_size *= 2;
#endif
#if defined (__x86_64 ) && defined (SOLARIS)
/*GK: the LDAP on that platform requires that */
stack_size *= 2;
#endif
#ifdef HPUX_ITANIUM64
stack_size += 8 * 8192;
#endif
stack_size = ((stack_size / 8192) + 1) * 8192;
#if defined (PTHREAD_STACK_MIN)
if (stack_size < PTHREAD_STACK_MIN)
{
stack_size = PTHREAD_STACK_MIN;
}
#endif
/* Any free threads with the right stack size? */
Q_LOCK ();
for (thr = (thread_t *) _deadq.thq_head.thr_next;
thr != (thread_t *) &_deadq.thq_head;
thr = (thread_t *) thr->thr_hdr.thr_next)
{
/* if (thr->thr_stack_size >= stack_size) */
break;
}
Q_UNLOCK ();
/* No free threads, create a new one */
if (thr == (thread_t *) &_deadq.thq_head)
{
#ifndef OLD_PTHREADS
size_t os_stack_size = stack_size;
#endif
thr = thread_alloc ();
thr->thr_initial_function = initial_function;
thr->thr_initial_argument = initial_argument;
thr->thr_stack_size = stack_size;
if (thr->thr_cv == NULL)
goto failed;
#ifdef HPUX_ITANIUM64
if (stack_size > PTHREAD_STACK_MIN)
{
size_t s, rses;
pthread_attr_getstacksize (&_thread_attr, &s);
pthread_attr_getrsestacksize_np (&_thread_attr, &rses);
log_error ("default rses=%d stack=%d : %m", rses,s);
}
#endif
#ifndef OLD_PTHREADS
# if defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE)
rc = pthread_attr_setstacksize (&_thread_attr, stack_size);
if (rc)
{
log_error ("Failed setting the OS thread stack size to %d : %m", stack_size);
}
# endif
#if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE)
if (0 == pthread_attr_getstacksize (&_thread_attr, &os_stack_size))
{
if (os_stack_size > 4 * 8192)
stack_size = thr->thr_stack_size = ((unsigned long) os_stack_size) - 4 * 8192;
}
#endif
#ifdef HPUX_ITANIUM64
if (stack_size > PTHREAD_STACK_MIN)
{
size_t rsestack_size = stack_size / 2;
rc = pthread_attr_setrsestacksize_np (&_thread_attr, rsestack_size);
if (rc)
{
log_error ("Failed setting the OS thread 'rse' stack size to %d (plain stack size set to %d) : %m", rsestack_size, stack_size);
}
thr->thr_stack_size /= 2;
}
#endif
rc = pthread_create ((pthread_t *) thr->thr_handle, &_thread_attr,
_thread_boot, thr);
CKRET (rc);
//.........这里部分代码省略.........
示例15: GC_pthread_create
int
GC_pthread_create(pthread_t *new_thread,
const pthread_attr_t *attr_in,
void * (*thread_execp)(void *), void *arg)
{
int result;
GC_thread t;
pthread_t my_new_thread;
pthread_attr_t attr;
word my_flags = 0;
int flag;
void * stack = 0;
size_t stack_size = 0;
int n;
struct sched_param schedparam;
(void)pthread_attr_init(&attr);
if (attr_in != 0) {
(void)pthread_attr_getstacksize(attr_in, &stack_size);
(void)pthread_attr_getstackaddr(attr_in, &stack);
}
LOCK();
if (!GC_is_initialized) {
GC_init_inner();
}
GC_multithreaded++;
if (stack == 0) {
if (stack_size == 0)
stack_size = 1048576;
/* ^-- 1 MB (this was GC_min_stack_sz, but that
* violates the pthread_create documentation which
* says the default value if none is supplied is
* 1MB) */
else
stack_size += thr_min_stack();
stack = (void *)GC_stack_alloc(&stack_size);
if (stack == 0) {
GC_multithreaded--;
UNLOCK();
errno = ENOMEM;
return -1;
}
} else {
my_flags |= CLIENT_OWNS_STACK;
}
(void)pthread_attr_setstacksize(&attr, stack_size);
(void)pthread_attr_setstackaddr(&attr, stack);
if (attr_in != 0) {
(void)pthread_attr_getscope(attr_in, &n);
(void)pthread_attr_setscope(&attr, n);
(void)pthread_attr_getschedparam(attr_in, &schedparam);
(void)pthread_attr_setschedparam(&attr, &schedparam);
(void)pthread_attr_getschedpolicy(attr_in, &n);
(void)pthread_attr_setschedpolicy(&attr, n);
(void)pthread_attr_getinheritsched(attr_in, &n);
(void)pthread_attr_setinheritsched(&attr, n);
(void)pthread_attr_getdetachstate(attr_in, &flag);
if (flag == PTHREAD_CREATE_DETACHED) {
my_flags |= DETACHED;
}
(void)pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
}
/*
* thr_create can call malloc(), which if redirected will
* attempt to acquire the allocation lock.
* Unlock here to prevent deadlock.
*/
#if 0
#ifdef I386
UNLOCK();
#endif
#endif
result =
pthread_create(&my_new_thread, &attr, thread_execp, arg);
#if 0
#ifdef I386
LOCK();
#endif
#endif
if (result == 0) {
t = GC_new_thread(my_new_thread);
t -> flags = my_flags;
if (!(my_flags & DETACHED)) cond_init(&(t->join_cv), USYNC_THREAD, 0);
t -> stack = stack;
t -> stack_size = stack_size;
if (new_thread != 0) *new_thread = my_new_thread;
pthread_cond_signal(&GC_create_cv);
} else {
if (!(my_flags & CLIENT_OWNS_STACK)) {
GC_stack_free(stack, stack_size);
}
GC_multithreaded--;
}
UNLOCK();
//.........这里部分代码省略.........