示例#1
0
int
main()
{
    pthread_t alloc_pth[N_ALLOC_TH], gcollect_pth;
    int i, err;

    cuoo_init();
    _obj_type = cuoo_type_new_opaque_hcs(NULL, sizeof(cu_word_t));

    err = GC_pthread_create(&gcollect_pth, NULL, _gcollect_proc, NULL);
    if (err) _fail(err, "GC_pthread_create");

    for (i = 0; i < N_ALLOC_TH; ++i) {
	err = GC_pthread_create(&alloc_pth[i], NULL, _alloc_proc, &i + i);
	if (err) _fail(err, "GC_pthread_create");
    }
    for (i = 0; i < N_ALLOC_TH; ++i) {
	err = GC_pthread_join(alloc_pth[i], NULL);
	if (err) _fail(err, "GC_pthread_join");
    }

    err = GC_pthread_join(gcollect_pth, NULL);
    if (err) _fail(err, "GC_pthread_join");

    return 2*!!cu_test_bug_count();
}
int main (void)
{
  int i;
  GC_INIT ();

# ifdef GC_SOLARIS_THREADS
    pthread_key_create (&key, on_thread_exit);
# else
    pthread_once (&key_once, make_key);
# endif
  for (i = 0; i < NTHREADS; i++) {
    pthread_t t;

    if (GC_pthread_create(&t, NULL, entry, NULL) == 0) {
      void *res;
      int code = (i & 1) != 0 ? GC_pthread_join(t, &res)
                                : GC_pthread_detach(t);

      if (code != 0) {
        fprintf(stderr, "Thread %s failed %d\n",
                (i & 1) != 0 ? "join" : "detach", code);
        exit(2);
      }
    }
  }
  return 0;
}
示例#3
0
EXTERN int neko_thread_create( thread_main_func init, thread_main_func main, void *param, void **handle ) {
	tparams p;
	p.init = init;
	p.main = main;
	p.param = param;
#	if !defined(NEKO_THREADS)
	return 0;
#	elif defined(NEKO_WINDOWS)
	{
		HANDLE h;
		p.lock = CreateSemaphore(NULL,0,1,NULL);
		h = GC_CreateThread(NULL,0,ThreadMain,&p,0,(void*)handle);
		if( h == NULL ) {
			CloseHandle(p.lock);
			return 0;
		}
		WaitForSingleObject(p.lock,INFINITE);
		CloseHandle(p.lock);
		CloseHandle(h);
		return 1;
	}
#	else
	pthread_mutex_init(&p.lock,NULL);
	pthread_mutex_lock(&p.lock);
	// force the use of a the GC method to capture created threads
	// this function should be defined in gc/gc.h
	if( GC_pthread_create((pthread_t*)handle,NULL,&ThreadMain,&p) != 0 ) {
		pthread_mutex_destroy(&p.lock);
		return 0;
	}
	pthread_mutex_lock(&p.lock);
	pthread_mutex_destroy(&p.lock);
	return 1;
#	endif
}
示例#4
0
void * GC_CALLBACK on_thread_exit_inner (struct GC_stack_base * sb, void * arg)
{
  int res = GC_register_my_thread (sb);
  pthread_t t;

  GC_pthread_create (&t, NULL, entry, NULL);
  if (res == GC_SUCCESS)
    GC_unregister_my_thread ();
  return NULL;
}
void _xspawn(caps_t caps, int len ,
             void * (*thread)(void *), void *arg, 
             int arg_len)
{
  pthread_attr_t attr;
  pthread_t id;
  int s;
 
  if ((s = pthread_attr_init(&attr)) != 0) {
    errno = s; perror("pthread_attr_init");
    errquit("internal error: _xspawn aborting\n");
  }

  if ((s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) != 0) {
    errno = s; perror("pthread_attr_setdetachstate");
    errquit("internal error: _xspawn aborting\n");
  }

  sinfo_t si = __init_new_thread_stack(caps,len,thread,arg,arg_len);

  //force "main" to wait for the new thread
  //TODO: if pthread_create fails throw 
  //an exception and atomically decrement all_done
  __sync_fetch_and_add(&all_done,1);

  //  if(pthread_attr_setstack(&attr,
  //             ((char *)si)+si->info.begin_offset,
  //           si->info.end_offset-si->info.begin_offset))
  //      errquit("pthread_attr_setstack ");

  if (stack_space > 0) {
    if ((s = pthread_attr_setstacksize(&attr,stack_space)) != 0) {
      errno = s; perror("pthread_attr_setstacksize");
      errquit("internal error: _xspawn aborting\n");
    }
  }

  if ((s = GC_pthread_create(&id,&attr,_thread_internal2,si)) != 0) {
    errno = s; perror("pthread_create");
    errquit("internal error: _xspawn aborting\n");
  }
  if ((s = pthread_attr_destroy(&attr)) != 0) {
    errno = s; perror("pthread_attr_destroy");
    errquit("internal error: _xspawn aborting\n");
  }
}
示例#6
0
文件: mzrt.c 项目: josephzizys/racket
mz_proc_thread* mz_proc_thread_create_w_stacksize(mz_proc_thread_start start_proc, void* data, intptr_t stacksize)
{
    mz_proc_thread *thread = (mz_proc_thread*)malloc(sizeof(mz_proc_thread));
    mzrt_thread_stub_data *stub_data;
    int ok;

#   ifndef WIN32
    pthread_attr_t *attr;
    pthread_attr_t attr_storage;

    if (stacksize) {
        attr = &attr_storage;
        pthread_attr_init(attr);
        pthread_attr_setstacksize(attr, stacksize); /*8MB*/
    } else
        attr = NULL;
#   endif

    thread->refcount = 2;

    stub_data = (mzrt_thread_stub_data*)malloc(sizeof(mzrt_thread_stub_data));

    stub_data->start_proc = start_proc;
    stub_data->data       = data;
    stub_data->thread     = thread;
#   ifdef WIN32
    thread->threadid = (HANDLE)_beginthreadex(NULL, stacksize, mzrt_win_thread_stub, stub_data, 0, NULL);
    ok = (thread->threadid != -1L);
#   else
#    ifdef NEED_GC_THREAD_OPS
    ok = !GC_pthread_create(&thread->threadid, attr, mzrt_thread_stub, stub_data);
#    else
    ok = !pthread_create(&thread->threadid, attr, mzrt_thread_stub, stub_data);
#    endif
#   endif

    if (!ok) {
        free(thread);
        free(stub_data);
        return NULL;
    }

    return thread;
}
示例#7
0
int main (void)
{
  int i;
  GC_INIT ();

# ifdef GC_SOLARIS_THREADS
    pthread_key_create (&key, on_thread_exit);
# else
    pthread_once (&key_once, make_key);
# endif
  for (i = 0; i < LIMIT; i++) {
    pthread_t t;
    void *res;
    if (GC_pthread_create (&t, NULL, entry, NULL) == 0
        && (i & 1) != 0)
      GC_pthread_join (t, &res);
  }
  return 0;
}
void * GC_CALLBACK on_thread_exit_inner (struct GC_stack_base * sb, void * arg)
{
  int res = GC_register_my_thread (sb);
  pthread_t t;
  int creation_res;     /* Used to suppress a warning about     */
                        /* unchecked pthread_create() result.   */
  pthread_attr_t attr;

  if (pthread_attr_init(&attr) != 0
      || pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
    fprintf(stderr, "Thread attribute init or setdetachstate failed\n");
    exit(2);
  }
  creation_res = GC_pthread_create(&t, &attr, entry, NULL);
  (void)pthread_attr_destroy(&attr);
  if (res == GC_SUCCESS)
    GC_unregister_my_thread ();

  return arg ? (void*)(GC_word)creation_res : 0;
}
示例#9
0
void create_profiler_thread(void * (* start)(void *))
{
  pthread_t id;
  long status = GC_pthread_create(&id, NULL, start, NULL);
  if (status != 0)
    {
      printf("GC_pthread_create status = %ld\n", status);
      fflush(stdout);
      signal_lisp_error("Unable to create profiler thread.");
      return;
    }
  status = GC_pthread_detach(id);
  if (status != 0)
    {
      printf("GC_pthread_detach status = %ld\n", status);
      fflush(stdout);
      signal_lisp_error("Unable to detach profiler thread.");
      return;
    }
  sched_yield();
}
示例#10
0
extern "C" void *pthread_create(void *a, void *b, void *c, void *d)
{
    if (c != (void*)GC_start_routine)
        return GC_pthread_create(a, b, c, d);
    else
        {
            if (pthread_lib == NULL)
                {
                    pthread_lib = dlopen("libpthread.so.0", RTLD_LAZY);
                    if (pthread_lib == NULL)
                        {
                            printf("cannot dynamically open pthreads %s.\n", dlerror());
                            exit(1);
                        }
                    orig = (pthread_create_type*)dlsym(pthread_lib, "pthread_create");
                    if (orig == NULL)
                        {
                            printf("cannot find symbol pthread_create %s.\n", dlerror());
                            exit(1);
                        }
                }
            return (*orig)(a, b, c, d);
        }
}
示例#11
0
static void *thread_new(void *(*Func)(void *), void *Arg) {
    pthread_t Thread;
    GC_pthread_create(&Thread, 0, Func, Arg);
    return (void *)Thread;
};