NdbThread.c 3.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


18
#include <ndb_global.h>
19
#include <NdbThread.h>
20
#include <pthread.h>
mronstrom@mysql.com's avatar
mronstrom@mysql.com committed
21
#include <NdbMem.h>
22 23 24

#define MAX_THREAD_NAME 16

25
/*#define USE_PTHREAD_EXTRAS*/
26

27 28 29 30
#ifdef NDB_SHM_TRANSPORTER
int g_ndb_shm_signum= 0;
#endif

31 32 33 34
struct NdbThread 
{ 
  pthread_t thread;
  char thread_name[MAX_THREAD_NAME];
joreland@mysql.com's avatar
joreland@mysql.com committed
35 36
  NDB_THREAD_FUNC * func;
  void * object;
37 38
};

joreland@mysql.com's avatar
joreland@mysql.com committed
39 40 41 42 43
static
void*
ndb_thread_wrapper(void* _ss){
  void * ret;
  struct NdbThread * ss = (struct NdbThread *)_ss;
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
44
  DBUG_ENTER("ndb_thread_wrapper");
joreland@mysql.com's avatar
joreland@mysql.com committed
45
#ifdef NDB_SHM_TRANSPORTER
46 47 48
  if (g_ndb_shm_signum)
  {
    sigset_t mask;
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
49
    DBUG_PRINT("info",("Block signum %d",g_ndb_shm_signum));
50 51 52 53
    sigemptyset(&mask);
    sigaddset(&mask, g_ndb_shm_signum);
    pthread_sigmask(SIG_BLOCK, &mask, 0);
  }
joreland@mysql.com's avatar
joreland@mysql.com committed
54 55
#endif
  ret= (* ss->func)(ss->object);
56
  DBUG_RETURN(ret);
joreland@mysql.com's avatar
joreland@mysql.com committed
57
}
58 59 60 61 62 63 64 65 66 67 68 69


struct NdbThread* NdbThread_Create(NDB_THREAD_FUNC *p_thread_func,
                      NDB_THREAD_ARG *p_thread_arg,
  		      const NDB_THREAD_STACKSIZE thread_stack_size,
		      const char* p_thread_name,
                      NDB_THREAD_PRIO thread_prio)
{
  struct NdbThread* tmpThread;
  int result;
  pthread_attr_t thread_attr;

70
  (void)thread_prio; /* remove warning for unused parameter */
71

72 73 74
  if (p_thread_func == NULL)
    return 0;

mronstrom@mysql.com's avatar
mronstrom@mysql.com committed
75
  tmpThread = (struct NdbThread*)NdbMem_Allocate(sizeof(struct NdbThread));
76 77 78
  if (tmpThread == NULL)
    return NULL;

79
  strnmov(tmpThread->thread_name,p_thread_name,sizeof(tmpThread->thread_name));
80 81

  pthread_attr_init(&thread_attr);
82 83 84
#if (SIZEOF_CHARP == 8)
  pthread_attr_setstacksize(&thread_attr, 2*thread_stack_size);
#else
85
  pthread_attr_setstacksize(&thread_attr, thread_stack_size);
86
#endif
87
#ifdef USE_PTHREAD_EXTRAS
88 89 90 91
  /* Guard stack overflow with a 2k databuffer */
  pthread_attr_setguardsize(&thread_attr, 2048);
#endif

92
#ifdef PTHREAD_CREATE_JOINABLE /* needed on SCO */
93
  pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
94
#endif
joreland@mysql.com's avatar
joreland@mysql.com committed
95 96
  tmpThread->func= p_thread_func;
  tmpThread->object= p_thread_arg;
97 98
  result = pthread_create(&tmpThread->thread, 
			  &thread_attr,
joreland@mysql.com's avatar
joreland@mysql.com committed
99 100
  		          ndb_thread_wrapper,
  		          tmpThread);
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  assert(result==0);

  pthread_attr_destroy(&thread_attr);
  return tmpThread;
}


void NdbThread_Destroy(struct NdbThread** p_thread)
{
  if (*p_thread != NULL){    
    free(* p_thread); 
    * p_thread = 0;
  }
}


int NdbThread_WaitFor(struct NdbThread* p_wait_thread, void** status)
{
  int result;

  if (p_wait_thread == NULL)
    return 0;

124
  if (p_wait_thread->thread == 0)
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    return 0;

  result = pthread_join(p_wait_thread->thread, status);
  
  return result;
}


void NdbThread_Exit(int status)
{
  pthread_exit(&status);
}


int NdbThread_SetConcurrencyLevel(int level)
{
141
#ifdef USE_PTHREAD_EXTRAS
142 143
  return pthread_setconcurrency(level);
#else
144
  (void)level; /* remove warning for unused parameter */
145 146 147
  return 0;
#endif
}