本文整理汇总了C++中pthread_attr_setstacksize函数的典型用法代码示例。如果您正苦于以下问题:C++ pthread_attr_setstacksize函数的具体用法?C++ pthread_attr_setstacksize怎么用?C++ pthread_attr_setstacksize使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pthread_attr_setstacksize函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
//.........这里部分代码省略.........
fl_set_object_label(form_enb[UE_id]->button_0,"DL Traffic ON");
}
else {
fl_set_button(form_enb[UE_id]->button_0,0);
fl_set_object_label(form_enb[UE_id]->button_0,"DL Traffic OFF");
}
}
}
else {
if (openair_daq_vars.use_ia_receiver) {
fl_set_button(form_ue[UE_id]->button_0,1);
fl_set_object_label(form_ue[UE_id]->button_0, "IA Receiver ON");
}
else {
fl_set_button(form_ue[UE_id]->button_0,0);
fl_set_object_label(form_ue[UE_id]->button_0, "IA Receiver OFF");
}
}
ret = pthread_create(&thread2, NULL, scope_thread, NULL);
printf("Scope thread created, ret=%d\n",ret);
}
#endif
#ifdef EMOS
ret = pthread_create(&thread3, NULL, emos_thread, NULL);
printf("EMOS thread created, ret=%d\n",ret);
#endif
rt_sleep_ns(10*FRAME_PERIOD);
#ifndef RTAI
pthread_attr_init (&attr_dlsch_threads);
pthread_attr_setstacksize(&attr_dlsch_threads,OPENAIR_THREAD_STACK_SIZE);
//attr_dlsch_threads.priority = 1;
sched_param_dlsch.sched_priority = sched_get_priority_max(SCHED_FIFO); //OPENAIR_THREAD_PRIORITY;
pthread_attr_setschedparam (&attr_dlsch_threads, &sched_param_dlsch);
pthread_attr_setschedpolicy (&attr_dlsch_threads, SCHED_FIFO);
#endif
// start the main thread
if (UE_flag == 1) {
/*
#ifdef RTAI
thread1 = rt_thread_create(UE_thread, NULL, 100000000);
#else
error_code = pthread_create(&thread1, &attr_dlsch_threads, UE_thread, NULL);
if (error_code!= 0) {
LOG_D(HW,"[lte-softmodem.c] Could not allocate UE_thread, error %d\n",error_code);
return(error_code);
}
else {
LOG_D(HW,"[lte-softmodem.c] Allocate UE_thread successful\n");
}
#endif
#ifdef DLSCH_THREAD
init_rx_pdsch_thread();
rt_sleep_ns(FRAME_PERIOD/10);
init_dlsch_threads();
#endif
printf("UE threads created\n");
*/
}
else {
#ifdef RTAI
thread0 = rt_thread_create(eNB_thread, NULL, 100000000);
示例2: alsa_init
/*static*/ int
alsa_init(cubeb ** context, char const * context_name)
{
cubeb * ctx;
int r;
int i;
int fd[2];
pthread_attr_t attr;
snd_pcm_t * dummy;
assert(context);
*context = NULL;
pthread_mutex_lock(&cubeb_alsa_mutex);
if (!cubeb_alsa_error_handler_set) {
snd_lib_error_set_handler(silent_error_handler);
cubeb_alsa_error_handler_set = 1;
}
pthread_mutex_unlock(&cubeb_alsa_mutex);
ctx = calloc(1, sizeof(*ctx));
assert(ctx);
ctx->ops = &alsa_ops;
r = pthread_mutex_init(&ctx->mutex, NULL);
assert(r == 0);
r = pipe(fd);
assert(r == 0);
for (i = 0; i < 2; ++i) {
fcntl(fd[i], F_SETFD, fcntl(fd[i], F_GETFD) | FD_CLOEXEC);
fcntl(fd[i], F_SETFL, fcntl(fd[i], F_GETFL) | O_NONBLOCK);
}
ctx->control_fd_read = fd[0];
ctx->control_fd_write = fd[1];
/* Force an early rebuild when alsa_run is first called to ensure fds and
nfds have been initialized. */
ctx->rebuild = 1;
r = pthread_attr_init(&attr);
assert(r == 0);
r = pthread_attr_setstacksize(&attr, 256 * 1024);
assert(r == 0);
r = pthread_create(&ctx->thread, &attr, alsa_run_thread, ctx);
assert(r == 0);
r = pthread_attr_destroy(&attr);
assert(r == 0);
/* Open a dummy PCM to force the configuration space to be evaluated so that
init_local_config_with_workaround can find and modify the default node. */
r = alsa_locked_pcm_open(&dummy, SND_PCM_STREAM_PLAYBACK, NULL);
if (r >= 0) {
alsa_locked_pcm_close(dummy);
}
ctx->is_pa = 0;
pthread_mutex_lock(&cubeb_alsa_mutex);
ctx->local_config = init_local_config_with_workaround(CUBEB_ALSA_PCM_NAME);
pthread_mutex_unlock(&cubeb_alsa_mutex);
if (ctx->local_config) {
ctx->is_pa = 1;
r = alsa_locked_pcm_open(&dummy, SND_PCM_STREAM_PLAYBACK, ctx->local_config);
/* If we got a local_config, we found a PA PCM. If opening a PCM with that
config fails with EINVAL, the PA PCM is too old for this workaround. */
if (r == -EINVAL) {
pthread_mutex_lock(&cubeb_alsa_mutex);
snd_config_delete(ctx->local_config);
pthread_mutex_unlock(&cubeb_alsa_mutex);
ctx->local_config = NULL;
} else if (r >= 0) {
alsa_locked_pcm_close(dummy);
}
}
*context = ctx;
return CUBEB_OK;
}
示例3: defined
/**
* Starts the thread. This method will signal to start the thread and
* return immediately. Note that the thread might not yet run when this
* method returns! The abstract method Main() is the entry point for the
* new thread. You have to implement the Main() method in your subclass.
*
* @see StartThread()
*/
int Thread::SignalStartThread() {
#if defined(WIN32)
LPVOID lpParameter;
hThread = CreateThread(
NULL, // no security attributes
MIN_STACK_SIZE,
__win32thread_launcher,
this,
0,
&lpThreadId);
if(hThread == NULL) {
std::cerr << "Thread creation failed: Error" << GetLastError() << std::endl << std::flush;
#if defined(WIN32_SIGNALSTARTTHREAD_WORKAROUND)
win32isRunning = false;
#else
RunningCondition.Set(false);
#endif
return -1;
}
return 0;
#else
// prepare the thread properties
int res = pthread_attr_setinheritsched(&__thread_attr, PTHREAD_EXPLICIT_SCHED);
if (res) {
std::cerr << "Thread creation failed: Could not inherit thread properties."
<< std::endl << std::flush;
RunningCondition.Set(false);
return res;
}
res = pthread_attr_setdetachstate(&__thread_attr, PTHREAD_CREATE_JOINABLE);
if (res) {
std::cerr << "Thread creation failed: Could not request a joinable thread."
<< std::endl << std::flush;
RunningCondition.Set(false);
return res;
}
res = pthread_attr_setscope(&__thread_attr, PTHREAD_SCOPE_SYSTEM);
if (res) {
std::cerr << "Thread creation failed: Could not request system scope for thread scheduling."
<< std::endl << std::flush;
RunningCondition.Set(false);
return res;
}
res = pthread_attr_setstacksize(&__thread_attr, MIN_STACK_SIZE);
if (res) {
std::cerr << "Thread creation failed: Could not set minimum stack size."
<< std::endl << std::flush;
RunningCondition.Set(false);
return res;
}
// Create and run the thread
res = pthread_create(&this->__thread_id, &__thread_attr, __pthread_launcher, this);
switch (res) {
case 0: // Success
break;
case EAGAIN:
std::cerr << "Thread creation failed: System doesn't allow to create another thread."
<< std::endl << std::flush;
RunningCondition.Set(false);
break;
case EPERM:
std::cerr << "Thread creation failed: You're lacking permisssions to set required scheduling policy and parameters."
<< std::endl << std::flush;
RunningCondition.Set(false);
break;
default:
std::cerr << "Thread creation failed: Unknown cause."
<< std::endl << std::flush;
RunningCondition.Set(false);
break;
}
return res;
#endif
}
示例4: data_init
void data_init(struct ts *ts) {
memset(ts, 0, sizeof(struct ts));
// Stream
ts->pat = ts_pat_alloc();
ts->curpat = ts_pat_alloc();
ts->genpat = ts_pat_alloc();
ts->cat = ts_cat_alloc();
ts->curcat = ts_cat_alloc();
ts->pmt = ts_pmt_alloc();
ts->curpmt = ts_pmt_alloc();
ts->sdt = ts_sdt_alloc();
ts->cursdt = ts_sdt_alloc();
ts->emm = ts_privsec_alloc();
ts->last_emm = ts_privsec_alloc();
ts->tmp_emm = ts_privsec_alloc();
ts->ecm = ts_privsec_alloc();
ts->last_ecm = ts_privsec_alloc();
ts->tmp_ecm = ts_privsec_alloc();
pidmap_clear(&ts->pidmap);
pidmap_clear(&ts->cc);
pidmap_clear(&ts->pid_seen);
// Key
memset(&ts->key, 0, sizeof(ts->key));
ts->key.csakey = csa_key_alloc();
gettimeofday(&ts->key.ts_keyset, NULL);
// CAMD
memset(&ts->camd, 0, sizeof(ts->camd));
ts->camd.server_fd = -1;
ts->camd.server_port = 2233;
ts->camd.key = &ts->key;
ts->camd.user = "user";
ts->camd.pass = "pass";
strcpy(ts->camd.newcamd.hex_des_key, "0102030405060708091011121314");
camd_proto_cs378x(&ts->camd.ops);
// Config
ts->syslog_port = 514;
ts->ts_discont = 1;
ts->ecm_cw_log = 1;
ts->debug_level = 0;
ts->req_CA_sys = CA_CONAX;
ts->emm_send = 0;
ts->pid_filter = 1;
ts->emm_report_interval = 60;
ts->emm_last_report = time(NULL);
ts->ecm_report_interval = 60;
ts->ecm_last_report = time(NULL);
ts->cw_warn_sec = 60;
ts->cw_last_warn= time(NULL);
ts->cw_last_warn= ts->cw_last_warn + ts->cw_warn_sec;
ts->key.ts = time(NULL);
ts->input.fd = 0; // STDIN
ts->input.type = FILE_IO;
ts->output.fd = 1; // STDOUT
ts->output.type = FILE_IO;
ts->output.ttl = 1;
ts->output.tos = -1;
ts->decode_buf = cbuf_init((7 * csa_get_batch_size() * 188) * 16, "decode"); // ~658Kb
ts->write_buf = cbuf_init((7 * csa_get_batch_size() * 188) * 8, "write"); // ~324Kb
ts->input_buffer= list_new("input");
pthread_attr_init(&ts->thread_attr);
size_t stack_size;
pthread_attr_getstacksize(&ts->thread_attr, &stack_size);
if (stack_size > THREAD_STACK_SIZE)
pthread_attr_setstacksize(&ts->thread_attr, THREAD_STACK_SIZE);
}
示例5: sam_hc_callback_register
cs_error_t sam_hc_callback_register (sam_hc_callback_t cb)
{
cs_error_t error = CS_OK;
pthread_attr_t thread_attr;
int pipe_error;
int pipe_fd[2];
if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED) {
return (CS_ERR_BAD_HANDLE);
}
if (sam_internal_data.time_interval == 0) {
return (CS_ERR_INVALID_PARAM);
}
if (sam_internal_data.cb_registered) {
sam_internal_data.hc_callback = cb;
return (CS_OK);
}
/*
* We know, this is first registration
*/
if (cb == NULL) {
return (CS_ERR_INVALID_PARAM);
}
pipe_error = pipe (pipe_fd);
if (pipe_error != 0) {
/*
* Pipe creation error
*/
error = CS_ERR_LIBRARY;
goto error_exit;
}
sam_internal_data.cb_rpipe_fd = pipe_fd[0];
sam_internal_data.cb_wpipe_fd = pipe_fd[1];
/*
* Create thread attributes
*/
error = pthread_attr_init (&thread_attr);
if (error != 0) {
error = CS_ERR_LIBRARY;
goto error_close_fd_exit;
}
pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
pthread_attr_setstacksize (&thread_attr, 32768);
/*
* Create thread
*/
error = pthread_create (&sam_internal_data.cb_thread, &thread_attr, hc_callback_thread, NULL);
if (error != 0) {
error = CS_ERR_LIBRARY;
goto error_attr_destroy_exit;
}
/*
* Cleanup
*/
pthread_attr_destroy(&thread_attr);
sam_internal_data.cb_registered = 1;
sam_internal_data.hc_callback = cb;
return (CS_OK);
error_attr_destroy_exit:
pthread_attr_destroy(&thread_attr);
error_close_fd_exit:
sam_internal_data.cb_rpipe_fd = sam_internal_data.cb_wpipe_fd = 0;
close (pipe_fd[0]);
close (pipe_fd[1]);
error_exit:
return (error);
}
示例6: px4_task_spawn_cmd
px4_task_t px4_task_spawn_cmd(const char *name, int scheduler, int priority, int stack_size, px4_main_t entry,
char *const argv[])
{
int i;
int argc = 0;
unsigned int len = 0;
struct sched_param param = {};
char *p = (char *)argv;
// Calculate argc
while (p != (char *)nullptr) {
p = argv[argc];
if (p == (char *)nullptr) {
break;
}
++argc;
len += strlen(p) + 1;
}
unsigned long structsize = sizeof(pthdata_t) + (argc + 1) * sizeof(char *);
// not safe to pass stack data to the thread creation
pthdata_t *taskdata = (pthdata_t *)malloc(structsize + len);
if (taskdata == nullptr) {
return -ENOMEM;
}
memset(taskdata, 0, structsize + len);
unsigned long offset = ((unsigned long)taskdata) + structsize;
strncpy(taskdata->name, name, 16);
taskdata->name[15] = 0;
taskdata->entry = entry;
taskdata->argc = argc;
for (i = 0; i < argc; i++) {
PX4_DEBUG("arg %d %s\n", i, argv[i]);
taskdata->argv[i] = (char *)offset;
strcpy((char *)offset, argv[i]);
offset += strlen(argv[i]) + 1;
}
// Must add NULL at end of argv
taskdata->argv[argc] = (char *)nullptr;
PX4_DEBUG("starting task %s", name);
pthread_attr_t attr;
int rv = pthread_attr_init(&attr);
if (rv != 0) {
PX4_ERR("px4_task_spawn_cmd: failed to init thread attrs");
free(taskdata);
return (rv < 0) ? rv : -rv;
}
#ifndef __PX4_DARWIN
if (stack_size < PTHREAD_STACK_MIN) {
stack_size = PTHREAD_STACK_MIN;
}
rv = pthread_attr_setstacksize(&attr, PX4_STACK_ADJUSTED(stack_size));
if (rv != 0) {
PX4_ERR("pthread_attr_setstacksize to %d returned error (%d)", stack_size, rv);
pthread_attr_destroy(&attr);
free(taskdata);
return (rv < 0) ? rv : -rv;
}
#endif
rv = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
if (rv != 0) {
PX4_ERR("px4_task_spawn_cmd: failed to set inherit sched");
pthread_attr_destroy(&attr);
free(taskdata);
return (rv < 0) ? rv : -rv;
}
rv = pthread_attr_setschedpolicy(&attr, scheduler);
if (rv != 0) {
PX4_ERR("px4_task_spawn_cmd: failed to set sched policy");
pthread_attr_destroy(&attr);
free(taskdata);
return (rv < 0) ? rv : -rv;
}
#ifdef __PX4_CYGWIN
/* Priorities on Windows are defined a lot differently */
priority = SCHED_PRIORITY_DEFAULT;
#endif
//.........这里部分代码省略.........
示例7: startStack
void startStack(uint64_t const rstacksize = 64*1024)
{
if ( ! thread.get() )
{
thread_ptr_type tthread(new pthread_t);
thread = UNIQUE_PTR_MOVE(tthread);
#if 0
std::cerr << "Creating thread without affinity." << std::endl;
std::cerr << ::libmaus2::util::StackTrace::getStackTrace() << std::endl;
#endif
pthread_attr_t attr;
if ( pthread_attr_init(&attr) )
{
::libmaus2::exception::LibMausException se;
se.getStream() << "pthread_attr_init failed:" << strerror(errno);
se.finish();
throw se;
}
#if defined(PTHREAD_STACK_MIN)
uint64_t const stacksize = std::max(rstacksize,static_cast<uint64_t>(PTHREAD_STACK_MIN));
#else
uint64_t const stacksize = std::max(rstacksize,static_cast<uint64_t>(64*1024));
#endif
if ( pthread_attr_setstacksize(&attr,stacksize) != 0 )
{
pthread_attr_destroy(&attr);
::libmaus2::exception::LibMausException se;
se.getStream() << "pthread_attr_setstacksize() failed in PosixThread::startStack(): " << strerror(errno) << std::endl;
se.finish();
throw se;
}
if ( pthread_create(thread.get(),&attr,dispatch,this) )
{
pthread_attr_destroy(&attr);
::libmaus2::exception::LibMausException se;
se.getStream() << "pthread_create() failed in PosixThread::start()";
se.finish();
throw se;
}
if ( pthread_attr_destroy(&attr) )
{
::libmaus2::exception::LibMausException se;
se.getStream() << "pthread_attr_destroy failed:" << strerror(errno);
se.finish();
throw se;
}
}
else
{
::libmaus2::exception::LibMausException se;
se.getStream() << "PosixThread::start() called but object is already in use.";
se.finish();
throw se;
}
}
示例8: memset
//rtems_task Init(rtems_task_argument Argument)
void *POSIX_Init(void *argument)
{
pthread_attr_t threadAttr;
pthread_t theThread;
struct sched_param sched_param;
size_t stack_size;
int result;
char data[1000];
memset(data, 1, sizeof(data));
/* Set the TOD clock, so that gettimeofday() will work */
rtems_time_of_day fakeTime = { 2006, 3, 15, 17, 30, 0, 0 };
if (RTEMS_SUCCESSFUL != rtems_clock_set(&fakeTime))
{
assert(0);
}
/* Bring up the network stack so we can run the socket tests. */
initialize_network();
/* Start a POSIX thread for pjlib_test_main(), since that's what it
* thinks it is running in.
*/
/* Initialize attribute */
TEST( pthread_attr_init(&threadAttr) );
/* Looks like the rest of the attributes must be fully initialized too,
* or otherwise pthread_create will return EINVAL.
*/
/* Specify explicit scheduling request */
TEST( pthread_attr_setinheritsched(&threadAttr, PTHREAD_EXPLICIT_SCHED));
/* Timeslicing is needed by thread test, and this is accomplished by
* SCHED_RR.
*/
TEST( pthread_attr_setschedpolicy(&threadAttr, SCHED_RR));
/* Set priority */
TEST( pthread_attr_getschedparam(&threadAttr, &sched_param));
sched_param.sched_priority = NETWORK_STACK_PRIORITY - 10;
TEST( pthread_attr_setschedparam(&threadAttr, &sched_param));
/* Must have sufficient stack size (large size is needed by
* logger, because default settings for logger is to use message buffer
* from the stack).
*/
TEST( pthread_attr_getstacksize(&threadAttr, &stack_size));
if (stack_size < 8192)
TEST( pthread_attr_setstacksize(&threadAttr, 8192));
/* Create the thread for application */
result = pthread_create(&theThread, &threadAttr, &pjlib_test_main, NULL);
if (result != 0) {
my_perror(PJ_STATUS_FROM_OS(result),
"Error creating pjlib_test_main thread");
assert(!"Error creating main thread");
}
return NULL;
}
示例9: pthread_attr_init
// create a new thread and attach to an activity
void SysThread::createThread()
{
pthread_attr_t newThreadAttr;
int schedpolicy, maxpri, minpri;
struct sched_param schedparam;
// Create an attr block for Thread.
pthread_attr_init(&newThreadAttr);
#if defined(LINUX) || defined(OPSYS_SUN) || defined(AIX)
/* scheduling on two threads controlled by the result method of the */
/* message object do not work properly without an enhanced priority */
pthread_getschedparam(pthread_self(), &schedpolicy, &schedparam);
#if defined(AIX)
// Starting with AIX 5.3 the priority for a thread created by
// a non root user can not be higher then 59. The priority
// of a user prog should not be higher then 59 (IBM AIX development).
schedparam.sched_priority = 59;
#else
# ifdef _POSIX_PRIORITY_SCHEDULING
maxpri = sched_get_priority_max(schedpolicy);
minpri = sched_get_priority_min(schedpolicy);
schedparam.sched_priority = (minpri + maxpri) / 2;
# endif
#endif
#if defined(OPSYS_SUN)
/* PTHREAD_EXPLICIT_SCHED ==> use scheduling attributes of the new object */
// pthread_attr_setinheritsched(&newThreadAttr, PTHREAD_EXPLICIT_SCHED);
/* Performance measurements show massive performance improvements > 50 % */
/* using Round Robin scheduling instead of FIFO scheduling */
pthread_attr_setschedpolicy(&newThreadAttr, SCHED_RR);
#endif
#if defined(AIX)
/* PTHREAD_EXPLICIT_SCHED ==> use scheduling attributes of the new object */
// pthread_attr_setinheritsched(&newThreadAttr, PTHREAD_EXPLICIT_SCHED);
/* Each thread has an initial priority that is dynamically modified by */
/* the scheduler, according to the thread's activity; thread execution */
/* is time-sliced. On other systems, this scheduling policy may be */
/* different. */
pthread_attr_setschedpolicy(&newThreadAttr, SCHED_OTHER);
#endif
pthread_attr_setschedparam(&newThreadAttr, &schedparam);
#endif
// Set the stack size.
pthread_attr_setstacksize(&newThreadAttr, THREAD_STACK_SIZE);
// Now create the thread
int rc = pthread_create(&_threadID, &newThreadAttr, call_thread_function, (void *)this);
if (rc != 0)
{
_threadID = 0;
fprintf(stderr," *** ERROR: At SysThread(), createThread - RC = %d !\n", rc);
}
pthread_attr_destroy(&newThreadAttr);
attached = false; // we own this thread
return;
}
示例10: vcos_thread_create
VCOS_STATUS_T vcos_thread_create(VCOS_THREAD_T *thread,
const char *name,
VCOS_THREAD_ATTR_T *attrs,
VCOS_THREAD_ENTRY_FN_T entry,
void *arg)
{
VCOS_STATUS_T st;
pthread_attr_t pt_attrs;
VCOS_THREAD_ATTR_T *local_attrs = attrs ? attrs : &default_attrs;
int rc;
vcos_assert(thread);
memset(thread, 0, sizeof(VCOS_THREAD_T));
rc = pthread_attr_init(&pt_attrs);
if (rc < 0)
return VCOS_ENOMEM;
st = vcos_semaphore_create(&thread->suspend, NULL, 0);
if (st != VCOS_SUCCESS)
{
pthread_attr_destroy(&pt_attrs);
return st;
}
pthread_attr_setstacksize(&pt_attrs, local_attrs->ta_stacksz);
#if VCOS_CAN_SET_STACK_ADDR
if (local_attrs->ta_stackaddr)
{
pthread_attr_setstackaddr(&pt_attrs, local_attrs->ta_stackaddr);
}
#else
vcos_demand(local_attrs->ta_stackaddr == 0);
#endif
/* pthread_attr_setpriority(&pt_attrs, local_attrs->ta_priority); */
vcos_assert(local_attrs->ta_stackaddr == 0); /* Not possible */
thread->entry = entry;
thread->arg = arg;
thread->legacy = local_attrs->legacy;
strncpy(thread->name, name, sizeof(thread->name));
thread->name[sizeof(thread->name)-1] = '\0';
memset(thread->at_exit, 0, sizeof(thread->at_exit));
rc = pthread_create(&thread->thread, &pt_attrs, vcos_thread_entry, thread);
pthread_attr_destroy(&pt_attrs);
if (rc < 0)
{
vcos_semaphore_delete(&thread->suspend);
return VCOS_ENOMEM;
}
else
{
return VCOS_SUCCESS;
}
}
示例11: StartServer
/* Might be called back from NovaWin_StartExecService */
void StartServer(Policy *policy, GenericAgentConfig *config, ExecConfig *exec_config, const ReportContext *report_context)
{
#if !defined(__MINGW32__)
time_t now = time(NULL);
#endif
Promise *pp = NewPromise("exec_cfengine", "the executor agent");
Attributes dummyattr;
CfLock thislock;
pthread_attr_init(&threads_attrs);
pthread_attr_setdetachstate(&threads_attrs, PTHREAD_CREATE_DETACHED);
pthread_attr_setstacksize(&threads_attrs, (size_t)2048*1024);
Banner("Starting executor");
memset(&dummyattr, 0, sizeof(dummyattr));
dummyattr.restart_class = "nonce";
dummyattr.transaction.ifelapsed = CF_EXEC_IFELAPSED;
dummyattr.transaction.expireafter = CF_EXEC_EXPIREAFTER;
if (!ONCE)
{
thislock = AcquireLock(pp->promiser, VUQNAME, CFSTARTTIME, dummyattr, pp, false);
if (thislock.lock == NULL)
{
DeletePromise(pp);
return;
}
/* Kill previous instances of cf-execd if those are still running */
Apoptosis();
/* FIXME: kludge. This code re-sets "last" lock to the one we have
acquired a few lines before. If the cf-execd is terminated, this lock
will be removed, and subsequent restart of cf-execd won't fail.
The culprit is Apoptosis(), which creates a promise and executes it,
taking locks during it, so CFLOCK/CFLAST/CFLOG get reset.
Proper fix is to keep all the taken locks in the memory, and release
all of them during process termination.
*/
strcpy(CFLOCK, thislock.lock);
strcpy(CFLAST, thislock.last ? thislock.last : "");
strcpy(CFLOG, thislock.log ? thislock.log : "");
}
#ifdef __MINGW32__
if (!NO_FORK)
{
CfOut(cf_verbose, "", "Windows does not support starting processes in the background - starting in foreground");
}
#else /* !__MINGW32__ */
if ((!NO_FORK) && (fork() != 0))
{
CfOut(cf_inform, "", "cf-execd starting %.24s\n", cf_ctime(&now));
_exit(0);
}
if (!NO_FORK)
{
ActAsDaemon(0);
}
#endif /* !__MINGW32__ */
WritePID("cf-execd.pid");
signal(SIGINT, HandleSignalsForDaemon);
signal(SIGTERM, HandleSignalsForDaemon);
signal(SIGHUP, SIG_IGN);
signal(SIGPIPE, SIG_IGN);
signal(SIGUSR1, HandleSignalsForDaemon);
signal(SIGUSR2, HandleSignalsForDaemon);
umask(077);
if (ONCE)
{
CfOut(cf_verbose, "", "Sleeping for splaytime %d seconds\n\n", SPLAYTIME);
sleep(SPLAYTIME);
LocalExec(exec_config);
CloseLog();
}
else
{
while (!IsPendingTermination())
{
if (ScheduleRun(&policy, config, exec_config, report_context))
{
CfOut(cf_verbose, "", "Sleeping for splaytime %d seconds\n\n", SPLAYTIME);
sleep(SPLAYTIME);
if (!LocalExecInThread(exec_config))
{
CfOut(cf_inform, "", "Unable to run agent in thread, falling back to blocking execution");
//.........这里部分代码省略.........
示例12: pthread_attr_init
sc_cor*
sc_cor_pkg_pthread::create( std::size_t stack_size, sc_cor_fn* fn, void* arg )
{
sc_cor_pthread* cor_p = new sc_cor_pthread;
DEBUGF << &main_cor << ": sc_cor_pkg_pthread::create("
<< cor_p << ")" << std::endl;
// INITIALIZE OBJECT'S FIELDS FROM ARGUMENT LIST:
cor_p->m_pkg_p = this;
cor_p->m_cor_fn = fn;
cor_p->m_cor_fn_arg = arg;
// SET UP THREAD CREATION ATTRIBUTES:
//
// Use default values except for stack size. If stack size is non-zero
// set it.
pthread_attr_t attr;
pthread_attr_init( &attr );
if ( stack_size != 0 )
{
pthread_attr_setstacksize( &attr, stack_size );
}
// ALLOCATE THE POSIX THREAD TO USE AND FORCE SEQUENTIAL EXECUTION:
//
// Because pthread_create causes the created thread to be executed,
// we need to let it run until we can block in the invoke_module_method.
// So we:
// (1) Lock the creation mutex before creating the new thread.
// (2) Sleep on the creation condition, which will be signalled by
// the newly created thread just before it goes to sleep in
// invoke_module_method.
// This scheme results in the newly created thread being dormant before
// the main thread continues execution.
pthread_mutex_lock( &create_mutex );
DEBUGF << &main_cor << ": about to create actual thread "
<< cor_p << std::endl;
if ( pthread_create( &cor_p->m_thread, &attr,
&sc_cor_pthread::invoke_module_method, (void*)cor_p ) )
{
std::fprintf(stderr, "ERROR - could not create thread\n");
}
DEBUGF << &main_cor << ": main thread waiting for signal from "
<< cor_p << std::endl;
pthread_cond_wait( &create_condition, &create_mutex );
DEBUGF << &main_cor << ": main thread signaled by "
<< cor_p << endl;
pthread_attr_destroy( &attr );
pthread_mutex_unlock( &create_mutex );
DEBUGF << &main_cor << ": exiting sc_cor_pkg_pthread::create("
<< cor_p << ")" << std::endl;
return cor_p;
}
示例13: main
int main( int argc, char **argv )
{
void *retval;
pthread_attr_t attr;
struct sched_param schedparam;
CYG_TEST_INIT();
sa.sin_family = AF_INET;
sa.sin_len = sizeof(sa);
inet_aton("127.0.0.1", &sa.sin_addr);
sa.sin_port = htons(1234);
init_all_network_interfaces();
// Create test threads
{
pthread_attr_init( &attr );
schedparam.sched_priority = 5;
pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
pthread_attr_setschedpolicy( &attr, SCHED_RR );
pthread_attr_setschedparam( &attr, &schedparam );
pthread_attr_setstackaddr( &attr, (void *)&thread1_stack[sizeof(thread1_stack)] );
pthread_attr_setstacksize( &attr, sizeof(thread1_stack) );
pthread_create( &thread1,
&attr,
pthread_entry1,
(void *)0x12345671);
}
{
pthread_attr_init( &attr );
schedparam.sched_priority = 10;
pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
pthread_attr_setschedpolicy( &attr, SCHED_RR );
pthread_attr_setschedparam( &attr, &schedparam );
pthread_attr_setstackaddr( &attr, (void *)&thread2_stack[sizeof(thread2_stack)] );
pthread_attr_setstacksize( &attr, sizeof(thread2_stack) );
pthread_create( &thread2,
&attr,
pthread_entry2,
(void *)0x12345672);
}
// Now join with thread1
CYG_TEST_INFO( "Main: calling pthread_join(thread1)");
pthread_join( thread1, &retval );
// And thread 2
CYG_TEST_INFO( "Main: calling pthread_join(thread2)");
pthread_join( thread2, &retval );
diag_printf("INFO: pselect returns: %d\n", pselect_wakeups );
diag_printf("INFO: pselect EINTR returns: %d\n", pselect_eintr );
diag_printf("INFO: SIGUSR1 sent: %d\n", sigusr1_sent );
diag_printf("INFO: SIGUSR1 delivered: %d\n", sigusr1_calls );
CYG_TEST_CHECK( sigusr1_sent == sigusr1_calls, "SIGUSR1 calls != delivered");
CYG_TEST_CHECK( sigusr1_sent == pselect_eintr, "SIGUSR1 calls != pselect EINTR wakeups");
CYG_TEST_PASS_FINISH("pselect");
}
示例14: init_pthread_attr
int init_pthread_attr(pthread_attr_t *pattr, const int stack_size)
{
size_t old_stack_size;
size_t new_stack_size;
int result;
if ((result=pthread_attr_init(pattr)) != 0)
{
/*logError("file: "__FILE__", line: %d, " \
"call pthread_attr_init fail, " \
"errno: %d, error info: %s", \
__LINE__, result, STRERROR(result));*/
return result;
}
if ((result=pthread_attr_getstacksize(pattr, &old_stack_size)) != 0)
{
/*logError("file: "__FILE__", line: %d, " \
"call pthread_attr_getstacksize fail, " \
"errno: %d, error info: %s", \
__LINE__, result, STRERROR(result));*/
return result;
}
if (stack_size > 0)
{
if (old_stack_size != stack_size)
{
new_stack_size = stack_size;
}
else
{
new_stack_size = 0;
}
}
else if (old_stack_size < 1 * 1024 * 1024)
{
new_stack_size = 1 * 1024 * 1024;
}
else
{
new_stack_size = 0;
}
if (new_stack_size > 0)
{
if ((result=pthread_attr_setstacksize(pattr, \
new_stack_size)) != 0)
{
/*logError("file: "__FILE__", line: %d, " \
"call pthread_attr_setstacksize fail, " \
"errno: %d, error info: %s", \
__LINE__, result, STRERROR(result));*/
return result;
}
}
if ((result=pthread_attr_setdetachstate(pattr, \
PTHREAD_CREATE_DETACHED)) != 0)
{
/*logError("file: "__FILE__", line: %d, " \
"call pthread_attr_setdetachstate fail, " \
"errno: %d, error info: %s", \
__LINE__, result, STRERROR(result));*/
return result;
}
return 0;
}
示例15: run_tests
McastResult* run_tests(int n_addr, int n_stream, char *start_addr, int startPort, int bufLen, int *jitterSize, int timeout, int verbose){
int i,j,rc;
int n_thread = n_addr * n_stream;
pthread_t thr[n_thread];
mthread_data_t thr_data[n_thread];
pthread_attr_t thread_attr;
pthread_attr_init(&thread_attr);
pthread_attr_setstacksize(&thread_attr , PTHREAD_STACK_MIN );
char *plural_s = "";
if (n_stream > 1) plural_s = "s";
char *plural_a = "";
if (n_addr > 1) plural_a = "es";
if (verbose == 1){
printf("Receiving from %d Multicast Address%s (starting at %s:%d) over %d stream%s.\n", n_addr, plural_a, start_addr, startPort, n_stream, plural_s);
}
for (i = 0; i < n_addr; i++){
for (j = 0; j < n_stream; j++){
int ind = i * n_stream + j;
sprintf(thr_data[ind].port, "%d", startPort + ind);
sprintf(thr_data[ind].addr, "%s", increment_address(start_addr, i));
thr_data[ind].bufLen = bufLen;
thr_data[ind].sock = sockets[ind];
thr_data[ind].jitterSize = *jitterSize;
thr_data[ind].timeout = timeout;
thr_data[ind].stat = createMcastStat(*jitterSize);
}
}
// loop again to ensure things start at same time.
for (i = 0; i < n_thread; i++){
if ((rc = pthread_create(&thr[i], &thread_attr, run_subtest, &thr_data[i])) < 0) {
printf("pthread_create rc: %d\n", rc);
fprintf(stderr, "error: pthread_create, rc: %d\n", rc);
return NULL;
}
}
int ntime = 0;
for (i = 0; i < n_thread; i++){
pthread_join(thr[i], NULL);
}
int nerr = 0;
McastStat *res = createMcastStat(*jitterSize);
for (i = 0; i < n_thread; i++){
McastStat *stat = thr_data[i].stat;
if (thr_data[i].timeout == -1){
nerr++;
}
else {
ntime = stat->ttime;
res->lost += stat->lost;
res->rcvd += stat->rcvd;
res->ttime += stat->ttime;
res->bytes += stat->bytes;
float rj = res->rollingJitter;
for (j = 0; j < stat->used; j++){
insertJitter(res, stat->jitters[j]);
}
res->rollingJitter = rj + stat->rollingJitter;
if (stat->used > *jitterSize){
*jitterSize = stat->used;
}
}
freeMcastStat(stat);
}
res->ttime += ntime * nerr;
if (nerr >= (n_thread + 1) / 2 || computeBitrate(res) < 0.01){
return (McastResult *)NULL;
}
res->rollingJitter /= (n_thread - nerr);
McastResult* r = computeMcastResult(res, n_addr, n_stream);
freeMcastStat(res);
return r;
}