Text file src/cmd/cgo/internal/test/cthread_unix.c

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
     6  
     7  #include <pthread.h>
     8  #include "_cgo_export.h"
     9  
    10  static void*
    11  addThread(void *p)
    12  {
    13  	int i, max;
    14  
    15  	max = *(int*)p;
    16  	for(i=0; i<max; i++)
    17  		Add(i);
    18  	return 0;
    19  }
    20  
    21  void
    22  doAdd(int max, int nthread)
    23  {
    24  	enum { MaxThread = 20 };
    25  	int i;
    26  	pthread_t thread_id[MaxThread];
    27  
    28  	if(nthread > MaxThread)
    29  		nthread = MaxThread;
    30  	for(i=0; i<nthread; i++)
    31  		pthread_create(&thread_id[i], 0, addThread, &max);
    32  	for(i=0; i<nthread; i++)
    33  		pthread_join(thread_id[i], 0);
    34  }
    35  
    36  static void*
    37  goDummyCallbackThread(void* p)
    38  {
    39  	int i, max;
    40  
    41  	max = *(int*)p;
    42  	for(i=0; i<max; i++)
    43  		goDummy();
    44  	return NULL;
    45  }
    46  
    47  int
    48  callGoInCThread(int max)
    49  {
    50  	pthread_t thread;
    51  
    52  	if (pthread_create(&thread, NULL, goDummyCallbackThread, (void*)(&max)) != 0)
    53  		return -1;
    54  	if (pthread_join(thread, NULL) != 0)
    55  		return -1;
    56  
    57  	return max;
    58  }
    59  

View as plain text