~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/src/common/portlib/NdbThread.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
 
 
17
#include <ndb_global.h>
 
18
#include <NdbThread.h>
 
19
#include <my_pthread.h>
 
20
#include <NdbMem.h>
 
21
 
 
22
#define MAX_THREAD_NAME 16
 
23
 
 
24
/*#define USE_PTHREAD_EXTRAS*/
 
25
 
 
26
#ifdef NDB_SHM_TRANSPORTER
 
27
int g_ndb_shm_signum= 0;
 
28
#endif
 
29
 
 
30
struct NdbThread 
 
31
 
32
  pthread_t thread;
 
33
  char thread_name[MAX_THREAD_NAME];
 
34
  NDB_THREAD_FUNC * func;
 
35
  void * object;
 
36
};
 
37
 
 
38
 
 
39
#ifdef NDB_SHM_TRANSPORTER
 
40
void NdbThread_set_shm_sigmask(my_bool block)
 
41
{
 
42
  DBUG_ENTER("NdbThread_set_shm_sigmask");
 
43
  if (g_ndb_shm_signum)
 
44
  {
 
45
    sigset_t mask;
 
46
    DBUG_PRINT("info",("Block signum %d",g_ndb_shm_signum));
 
47
    sigemptyset(&mask);
 
48
    sigaddset(&mask, g_ndb_shm_signum);
 
49
    if (block)
 
50
      pthread_sigmask(SIG_BLOCK, &mask, 0);
 
51
    else
 
52
      pthread_sigmask(SIG_UNBLOCK, &mask, 0);
 
53
  }
 
54
  DBUG_VOID_RETURN;
 
55
}
 
56
#endif
 
57
 
 
58
 
 
59
static
 
60
void*
 
61
ndb_thread_wrapper(void* _ss){
 
62
  my_thread_init();
 
63
  {
 
64
    DBUG_ENTER("ndb_thread_wrapper");
 
65
#ifdef NDB_SHM_TRANSPORTER
 
66
    NdbThread_set_shm_sigmask(TRUE);
 
67
#endif
 
68
    {
 
69
      /**
 
70
       * Block all signals to thread by default
 
71
       *   let them go to main process instead
 
72
       */
 
73
      sigset_t mask;
 
74
      sigfillset(&mask);
 
75
      pthread_sigmask(SIG_BLOCK, &mask, 0);
 
76
    }      
 
77
    
 
78
    {
 
79
      void *ret;
 
80
      struct NdbThread * ss = (struct NdbThread *)_ss;
 
81
      ret= (* ss->func)(ss->object);
 
82
      DBUG_POP();
 
83
      NdbThread_Exit(ret);
 
84
    }
 
85
  /* will never be reached */
 
86
    DBUG_RETURN(0);
 
87
  }
 
88
}
 
89
 
 
90
 
 
91
struct NdbThread* NdbThread_Create(NDB_THREAD_FUNC *p_thread_func,
 
92
                      NDB_THREAD_ARG *p_thread_arg,
 
93
                      const NDB_THREAD_STACKSIZE _thread_stack_size,
 
94
                      const char* p_thread_name,
 
95
                      NDB_THREAD_PRIO thread_prio)
 
96
{
 
97
  struct NdbThread* tmpThread;
 
98
  int result;
 
99
  pthread_attr_t thread_attr;
 
100
  NDB_THREAD_STACKSIZE thread_stack_size= _thread_stack_size * SIZEOF_CHARP/4;
 
101
 
 
102
  DBUG_ENTER("NdbThread_Create");
 
103
 
 
104
  (void)thread_prio; /* remove warning for unused parameter */
 
105
 
 
106
  if (p_thread_func == NULL)
 
107
    DBUG_RETURN(NULL);
 
108
 
 
109
  tmpThread = (struct NdbThread*)NdbMem_Allocate(sizeof(struct NdbThread));
 
110
  if (tmpThread == NULL)
 
111
    DBUG_RETURN(NULL);
 
112
 
 
113
  DBUG_PRINT("info",("thread_name: %s", p_thread_name));
 
114
 
 
115
  strnmov(tmpThread->thread_name,p_thread_name,sizeof(tmpThread->thread_name));
 
116
 
 
117
  pthread_attr_init(&thread_attr);
 
118
#ifdef PTHREAD_STACK_MIN
 
119
  if (thread_stack_size < PTHREAD_STACK_MIN)
 
120
    thread_stack_size = PTHREAD_STACK_MIN;
 
121
#endif
 
122
  pthread_attr_setstacksize(&thread_attr, thread_stack_size);
 
123
#ifdef USE_PTHREAD_EXTRAS
 
124
  /* Guard stack overflow with a 2k databuffer */
 
125
  pthread_attr_setguardsize(&thread_attr, 2048);
 
126
#endif
 
127
 
 
128
#ifdef PTHREAD_CREATE_JOINABLE /* needed on SCO */
 
129
  pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
 
130
#endif
 
131
  tmpThread->func= p_thread_func;
 
132
  tmpThread->object= p_thread_arg;
 
133
  result = pthread_create(&tmpThread->thread, 
 
134
                          &thread_attr,
 
135
                          ndb_thread_wrapper,
 
136
                          tmpThread);
 
137
  if (result != 0)
 
138
  {
 
139
    NdbMem_Free((char *)tmpThread);
 
140
    tmpThread = 0;
 
141
  }
 
142
 
 
143
  pthread_attr_destroy(&thread_attr);
 
144
  DBUG_PRINT("exit",("ret: 0x%lx", (long) tmpThread));
 
145
  DBUG_RETURN(tmpThread);
 
146
}
 
147
 
 
148
 
 
149
void NdbThread_Destroy(struct NdbThread** p_thread)
 
150
{
 
151
  DBUG_ENTER("NdbThread_Destroy");
 
152
  if (*p_thread != NULL){
 
153
    DBUG_PRINT("enter",("*p_thread: 0x%lx", (long) *p_thread));
 
154
    free(* p_thread); 
 
155
    * p_thread = 0;
 
156
  }
 
157
  DBUG_VOID_RETURN;
 
158
}
 
159
 
 
160
 
 
161
int NdbThread_WaitFor(struct NdbThread* p_wait_thread, void** status)
 
162
{
 
163
  int result;
 
164
 
 
165
  if (p_wait_thread == NULL)
 
166
    return 0;
 
167
 
 
168
  if (p_wait_thread->thread == 0)
 
169
    return 0;
 
170
 
 
171
  result = pthread_join(p_wait_thread->thread, status);
 
172
  
 
173
  return result;
 
174
}
 
175
 
 
176
 
 
177
void NdbThread_Exit(void *status)
 
178
{
 
179
  my_thread_end();
 
180
  pthread_exit(status);
 
181
}
 
182
 
 
183
 
 
184
int NdbThread_SetConcurrencyLevel(int level)
 
185
{
 
186
#ifdef USE_PTHREAD_EXTRAS
 
187
  return pthread_setconcurrency(level);
 
188
#else
 
189
  (void)level; /* remove warning for unused parameter */
 
190
  return 0;
 
191
#endif
 
192
}