ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/utils.C
(Generate patch)

Comparing deliantra/server/common/utils.C (file contents):
Revision 1.62 by root, Tue Jul 10 07:31:20 2007 UTC vs.
Revision 1.107 by root, Sat Nov 17 23:33:17 2018 UTC

1/* 1/*
2 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 5 *
8 * Crossfire TRT is free software: you can redistribute it and/or modify 6 * Deliantra is free software: you can redistribute it and/or modify it under
9 * it under the terms of the GNU General Public License as published by 7 * the terms of the Affero GNU General Public License as published by the
10 * the Free Software Foundation, either version 3 of the License, or 8 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 9 * option) any later version.
12 * 10 *
13 * This program is distributed in the hope that it will be useful, 11 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 14 * GNU General Public License for more details.
17 * 15 *
18 * You should have received a copy of the GNU General Public License 16 * You should have received a copy of the Affero GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * and the GNU General Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
20 * 19 *
21 * The authors can be reached via e-mail to <crossfire@schmorp.de> 20 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 21 */
23 22
24/* 23/*
25 * General convenience functions for crossfire. 24 * General convenience functions for deliantra.
26 */ 25 */
27 26
28#include <cstdlib> 27#include <cstdlib>
29#include <sys/types.h> 28#include <sys/types.h>
30#include <unistd.h> 29#include <unistd.h>
31#include <sys/time.h> 30#include <sys/time.h>
32#include <time.h> 31#include <time.h>
33#include <signal.h> 32#include <signal.h>
34 33
35#include <global.h> 34#include <global.h>
36#include <funcpoint.h>
37#include <material.h> 35#include <material.h>
36#include <object.h>
37
38#include <sys/time.h>
39#include <sys/resource.h>
38 40
39#include <glib.h> 41#include <glib.h>
40 42
41rand_gen rndm; 43refcnt_base::refcnt_t refcnt_dummy;
44ssize_t slice_alloc;
42 45
43void 46/******************************************************************************/
44tausworthe_random_generator::seed (uint32_t seed)
45{
46 state [0] = seed * 69069U; if (state [0] < 2U) state [0] += 2U;
47 state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U;
48 state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U;
49 state [3] = state [2] * 69069U; if (state [0] < 128) state [0] += 128U;
50 47
51 for (int i = 11; --i; ) 48/* Checks a player-provided string which will become the msg property of
52 operator ()(); 49 * an object for dangerous input.
53}
54
55uint32_t
56tausworthe_random_generator::next ()
57{
58 state [0] = ((state [0] & 0xFFFFFFFEU) << 18U) ^ (((state [0] << 6U) ^ state [0]) >> 13U);
59 state [1] = ((state [1] & 0xFFFFFFF8U) << 2U) ^ (((state [1] << 2U) ^ state [1]) >> 27U);
60 state [2] = ((state [2] & 0xFFFFFFF0U) << 7U) ^ (((state [2] << 13U) ^ state [2]) >> 21U);
61 state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U);
62
63 return state [0] ^ state [1] ^ state [2] ^ state [3];
64}
65
66uint32_t
67tausworthe_random_generator::get_range (uint32_t num)
68{
69 return (next () * (uint64_t)num) >> 32U;
70}
71
72// return a number within (min .. max)
73int
74tausworthe_random_generator::get_range (int r_min, int r_max)
75{
76 return r_min + get_range (max (r_max - r_min + 1, 0));
77}
78
79/*
80 * The random functions here take luck into account when rolling random
81 * dice or numbers. This function has less of an impact the larger the
82 * difference becomes in the random numbers. IE, the effect is lessened
83 * on a 1-1000 roll, vs a 1-6 roll. This can be used by crafty programmers,
84 * to specifically disable luck in certain rolls, simply by making the
85 * numbers larger (ie, 1d1000 > 500 vs 1d6 > 3)
86 */ 50 */
87 51bool
88/* 52msg_is_safe (const char *msg)
89 * Roll a random number between min and max. Uses op to determine luck,
90 * and if goodbad is non-zero, luck increases the roll, if zero, it decreases.
91 * Generally, op should be the player/caster/hitter requesting the roll,
92 * not the recipient (ie, the poor slob getting hit). [garbled 20010916]
93 */
94int
95random_roll (int r_min, int r_max, const object *op, int goodbad)
96{ 53{
97 int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ 54 bool safe = true;
98 55
99 if (r_max < r_min) 56 /* Trying to cheat by getting data into the object */
100 { 57 if (!strncmp (msg, "endmsg", sizeof ("endmsg") - 1)
101 LOG (llevError | logBacktrace, "Calling random_roll with min=%d max=%d\n", r_min, r_max); 58 || strstr (msg, "\nendmsg"))
102 return r_min; 59 safe = false;
103 }
104 60
105 if (op->type == PLAYER) 61 /* Trying to make the object talk, and potentially access arbitrary code */
106 { 62 if (object::msg_has_dialogue (msg))
107 int luck = op->stats.luck; 63 safe = false;
108 64
109 if (rndm (base) < min (10, abs (luck)))
110 {
111 //TODO: take luck into account
112 }
113 }
114
115 return rndm (r_min, r_max);
116}
117
118/*
119 * This is a 64 bit version of random_roll above. This is needed
120 * for exp loss calculations for players changing religions.
121 */
122sint64
123random_roll64 (sint64 min, sint64 max, const object *op, int goodbad)
124{
125 sint64 omin = min;
126 sint64 diff = max - min + 1;
127 int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */
128
129 if (diff < 0)
130 {
131 LOG (llevError | logBacktrace, "Calling random_roll64 with min=%" PRId64 " max=%" PRId64 "\n", min, max);
132 return (min); /* avoids a float exception */
133 }
134
135 /*
136 * Make a call to get two 32 bit unsigned random numbers, and just to
137 * a little bitshifting.
138 */
139 sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31);
140
141 if (op->type != PLAYER)
142 return ((ran % diff) + min);
143
144 int luck = op->stats.luck;
145
146 if (rndm (base) < MIN (10, abs (luck)))
147 {
148 /* we have a winner */
149 ((luck > 0) ? (luck = 1) : (luck = -1));
150 diff -= luck;
151 if (diff < 1)
152 return (omin); /*check again */
153
154 ((goodbad) ? (min += luck) : (diff));
155
156 return (MAX (omin, MIN (max, (ran % diff) + min)));
157 }
158
159 return ran % diff + min;
160}
161
162/*
163 * Roll a number of dice (2d3, 4d6). Uses op to determine luck,
164 * If goodbad is non-zero, luck increases the roll, if zero, it decreases.
165 * Generally, op should be the player/caster/hitter requesting the roll,
166 * not the recipient (ie, the poor slob getting hit).
167 * The args are num D size (ie 4d6) [garbled 20010916]
168 */
169int
170die_roll (int num, int size, const object *op, int goodbad)
171{
172 int min, luck, total, i, gotlucky;
173
174 int diff = size;
175 min = 1;
176 luck = total = gotlucky = 0;
177 int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */
178
179 if (size < 2 || diff < 1)
180 {
181 LOG (llevError, "Calling die_roll with num=%d size=%d\n", num, size);
182 return num; /* avoids a float exception */
183 }
184
185 if (op->type == PLAYER)
186 luck = op->stats.luck;
187
188 for (i = 0; i < num; i++)
189 {
190 if (rndm (base) < MIN (10, abs (luck)) && !gotlucky)
191 {
192 /* we have a winner */
193 gotlucky++;
194 ((luck > 0) ? (luck = 1) : (luck = -1));
195 diff -= luck;
196 if (diff < 1)
197 return (num); /*check again */
198 ((goodbad) ? (min += luck) : (diff));
199 total += MAX (1, MIN (size, rndm (diff) + min));
200 }
201 else
202 total += rndm (size) + 1;
203 }
204
205 return total;
206}
207
208/* convert materialname to materialtype_t */
209
210materialtype_t *
211name_to_material (const shstr &name)
212{
213 for (materialtype_t *mt = materialt; mt && mt->next; mt = mt->next)
214 if (name == mt->name)
215 return mt;
216
217 return 0; 65 return safe;
218}
219
220/* when doing transmutation of objects, we have to recheck the resistances,
221 * as some that did not apply previously, may apply now.
222 */
223
224void
225transmute_materialname (object *op, const object *change)
226{
227 materialtype_t *mt;
228 int j;
229
230 if (op->materialname == NULL)
231 return;
232
233 if (change->materialname != NULL && strcmp (op->materialname, change->materialname))
234 return;
235
236 if (!op->is_armor ())
237 return;
238
239 mt = name_to_material (op->materialname);
240 if (!mt)
241 {
242 LOG (llevError, "archetype '%s>%s' uses nonexistent material '%s'\n", &op->arch->archname, &op->name, &op->materialname);
243 return;
244 }
245
246 for (j = 0; j < NROFATTACKS; j++)
247 if (op->resist[j] == 0 && change->resist[j] != 0)
248 {
249 op->resist[j] += mt->mod[j];
250 if (op->resist[j] > 100)
251 op->resist[j] = 100;
252 if (op->resist[j] < -100)
253 op->resist[j] = -100;
254 }
255}
256
257/* set the materialname and type for an item */
258void
259set_materialname (object *op, int difficulty, materialtype_t *nmt)
260{
261 materialtype_t *mt, *lmt;
262
263 if (op->materialname != NULL)
264 return;
265
266 if (nmt == NULL)
267 {
268 lmt = NULL;
269
270 for (mt = materialt; mt && mt->next; mt = mt->next)
271 if (op->materials & mt->material && rndm (1, 100) <= mt->chance &&
272 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
273 {
274 lmt = mt;
275 if (!(op->is_weapon () || op->is_armor ()))
276 break;
277 }
278 }
279 else
280 lmt = nmt;
281
282 if (lmt != NULL)
283 {
284 if (op->stats.dam && op->is_weapon ())
285 {
286 op->stats.dam += lmt->damage;
287 if (op->stats.dam < 1)
288 op->stats.dam = 1;
289 }
290
291 if (op->stats.sp && op->type == BOW)
292 op->stats.sp += lmt->sp;
293 if (op->stats.wc && op->is_weapon ())
294 op->stats.wc += lmt->wc;
295 if (op->is_armor ())
296 {
297 if (op->stats.ac)
298 op->stats.ac += lmt->ac;
299
300 for (int j = 0; j < NROFATTACKS; j++)
301 if (op->resist[j] != 0)
302 {
303 op->resist[j] += lmt->mod[j];
304 if (op->resist[j] > 100)
305 op->resist[j] = 100;
306 if (op->resist[j] < -100)
307 op->resist[j] = -100;
308 }
309 }
310
311 op->materialname = lmt->name;
312 /* dont make it unstackable if it doesn't need to be */
313 if (op->is_weapon () || op->is_armor ())
314 {
315 op->weight = (op->weight * lmt->weight) / 100;
316 op->value = (op->value * lmt->value) / 100;
317 }
318 }
319}
320
321/*
322 * Strip out the media tags from a String.
323 * Warning the input string will contain the result string
324 */
325void
326strip_media_tag (char *message)
327{
328 int in_tag = 0;
329 char *dest;
330 char *src;
331
332 src = dest = message;
333 while (*src != '\0')
334 {
335 if (*src == '[')
336 {
337 in_tag = 1;
338 }
339 else if (in_tag && (*src == ']'))
340 in_tag = 0;
341 else if (!in_tag)
342 {
343 *dest = *src;
344 dest++;
345 }
346 src++;
347 }
348 *dest = '\0';
349}
350
351const char *
352strrstr (const char *haystack, const char *needle)
353{
354 const char *lastneedle;
355
356 lastneedle = NULL;
357 while ((haystack = strstr (haystack, needle)) != NULL)
358 {
359 lastneedle = haystack;
360 haystack++;
361 }
362 return lastneedle;
363
364}
365
366#define EOL_SIZE (sizeof("\n")-1)
367void
368strip_endline (char *buf)
369{
370 if (strlen (buf) < sizeof ("\n"))
371 {
372 return;
373 }
374 if (!strcmp (buf + strlen (buf) - EOL_SIZE, "\n"))
375 buf[strlen (buf) - EOL_SIZE] = '\0';
376}
377
378/**
379 * Replace in string src all occurrences of key by replacement. The resulting
380 * string is put into result; at most resultsize characters (including the
381 * terminating null character) will be written to result.
382 */
383void
384replace (const char *src, const char *key, const char *replacement, char *result, size_t resultsize)
385{
386 size_t resultlen;
387 size_t keylen;
388
389 /* special case to prevent infinite loop if key==replacement=="" */
390 if (strcmp (key, replacement) == 0)
391 {
392 snprintf (result, resultsize, "%s", src);
393 return;
394 }
395
396 keylen = strlen (key);
397
398 resultlen = 0;
399 while (*src != '\0' && resultlen + 1 < resultsize)
400 {
401 if (strncmp (src, key, keylen) == 0)
402 {
403 snprintf (result + resultlen, resultsize - resultlen, "%s", replacement);
404 resultlen += strlen (result + resultlen);
405 src += keylen;
406 }
407 else
408 {
409 result[resultlen++] = *src++;
410 }
411 }
412 result[resultlen] = '\0';
413}
414
415/**
416 * Taking a string as an argument, mutate it into a string that looks like a list.
417 * a 'list' for the purposes here, is a string of items, seperated by commas, except
418 * for the last entry, which has an 'and' before it, and a full stop (period) after it.
419 * This function will also strip all trailing non alphanumeric characters.
420 * It does not insert an oxford comma.
421 */
422
423void
424make_list_like (char *input)
425{
426 char *p, tmp[MAX_BUF];
427 int i;
428
429 if (!input || strlen (input) > MAX_BUF - 5)
430 return;
431 /* bad stuff would happen if we continued here, the -5 is to make space for ' and ' */
432
433 strncpy (tmp, input, MAX_BUF - 5);
434 /*trim all trailing commas, spaces etc. */
435 for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--)
436 tmp[i] = '\0';
437
438 strcat (tmp, ".");
439
440 p = strrchr (tmp, ',');
441 if (p)
442 {
443 *p = '\0';
444 strcpy (input, tmp);
445 p++;
446 strcat (input, " and");
447 strcat (input, p);
448 }
449 else
450 strcpy (input, tmp);
451
452 return;
453} 66}
454 67
455///////////////////////////////////////////////////////////////////////////// 68/////////////////////////////////////////////////////////////////////////////
456 69
457void 70void
458fork_abort (const char *msg) 71fork_abort (const char *msg)
459{ 72{
460 if (!fork ()) 73 if (!fork ())
461 { 74 {
75 signal (SIGINT , SIG_IGN);
76 signal (SIGTERM, SIG_IGN);
462 signal (SIGABRT, SIG_DFL); 77 signal (SIGABRT, SIG_IGN);
78
79 signal (SIGSEGV, SIG_DFL);
80 signal (SIGFPE , SIG_DFL);
81#ifdef SIGBUS
82 signal (SIGBUS , SIG_DFL);
83#endif
84 signal (SIGILL , SIG_DFL);
85 signal (SIGTRAP, SIG_DFL);
86
463 // try to put corefiles into a subdirectory, if existing, to allow 87 // try to put corefiles into a subdirectory, if existing, to allow
464 // an administrator to reduce the I/O load. 88 // an administrator to reduce the I/O load.
465 chdir ("cores"); 89 chdir ("cores");
90
91 // try to detach us from as many external dependencies as possible
92 // as coredumping can take time by closing all fd's.
93 {
94 struct rlimit lim;
95
96 if (getrlimit (RLIMIT_NOFILE, &lim))
97 lim.rlim_cur = 1024;
98
99 for (int i = 0; i < lim.rlim_cur; ++i)
100 close (i);
101 }
102
103 {
104 sigset_t empty;
105 sigemptyset (&empty);
106 sigprocmask (SIG_SETMASK, &empty, 0);
107 }
108
109 // try to coredump with SIGTRAP
110 kill (getpid (), SIGTRAP);
466 abort (); 111 abort ();
467 } 112 }
468 113
469 LOG (llevError, "fork abort: %s\n", msg); 114 LOG (llevError, "fork abort: %s\n", msg);
470} 115}
471 116
472void *salloc_ (int n) throw (std::bad_alloc) 117void *
118salloc_ (int n)
473{ 119{
474#ifdef PREFER_MALLOC
475 void *ptr = malloc (n);
476#else
477 void *ptr = g_slice_alloc (n); 120 void *ptr = g_slice_alloc (n);
478#endif
479 121
480 if (!ptr) 122 if (!ptr)
481 throw std::bad_alloc (); 123 throw std::bad_alloc ();
482 124
125 slice_alloc += n;
483 return ptr; 126 return ptr;
484} 127}
485 128
486void *salloc_ (int n, void *src) throw (std::bad_alloc) 129void *
130salloc_ (int n, void *src)
487{ 131{
488 void *ptr = salloc_ (n); 132 void *ptr = salloc_ (n);
489 133
490 if (src) 134 if (src)
491 memcpy (ptr, src, n); 135 memcpy (ptr, src, n);
493 memset (ptr, 0, n); 137 memset (ptr, 0, n);
494 138
495 return ptr; 139 return ptr;
496} 140}
497 141
142/******************************************************************************/
143
144#if DEBUG_SALLOC
145
146#define MAGIC 0xa1b2c35543deadLL
147
148void *
149g_slice_alloc (unsigned long size)
150{
151 unsigned long *p = (unsigned long *) (g_slice_alloc)(size + sizeof (unsigned long));
152 *p++ = size ^ MAGIC;
153 //fprintf (stderr, "g_slice_alloc %ld %p\n", size, p);//D
154 return (void *)p;
155}
156
157void *
158g_slice_alloc0 (unsigned long size)
159{
160 return memset (g_slice_alloc (size), 0, size);
161}
162
163void
164g_slice_free1 (unsigned long size, void *ptr)
165{
166 //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D
167 if (expect_true (ptr))
168 {
169 unsigned long *p = (unsigned long *)ptr;
170 unsigned long s = *--p ^ MAGIC;
171
172 if (size != (unsigned long)(*p ^ MAGIC))
173 {
174 LOG (logBacktrace | llevError, "slice free size (%lx) doesn't match alloc size (%lx)\n", size, s);
175 abort ();
176 }
177
178 *p = MAGIC;
179
180 (g_slice_free1)(s + sizeof (unsigned long), p);
181 }
182}
183
184#endif
185
186/******************************************************************************/
187
188refcnt_buf::refcnt_buf (size_t size)
189{
190 static uint32_t empty_buf [2] = { 0, 1 }; // 2 == never deallocated
191 data = (char *)empty_buf + overhead;
192 assert (overhead == sizeof (empty_buf));
193 inc ();
194}
195
196refcnt_buf::refcnt_buf (void *data, size_t size)
197{
198 _alloc (size);
199 memcpy (this->data, data, size);
200}
201
202refcnt_buf::~refcnt_buf ()
203{
204 dec ();
205}
206
207void
208refcnt_buf::_dealloc ()
209{
210 sfree<char> (data - overhead, size () + overhead);
211}
212
213refcnt_buf &
214refcnt_buf::operator =(const refcnt_buf &src)
215{
216 dec ();
217 data = src.data;
218 inc ();
219 return *this;
220}
221
222/******************************************************************************/
223
224int
498void assign (char *dst, const char *src, int maxlen) 225assign (char *dst, const char *src, int maxsize)
499{ 226{
500 if (!src) 227 if (!src)
501 src = ""; 228 src = "";
502 229
503 int len = strlen (src); 230 int len = strlen (src);
504 231
505 if (len >= maxlen - 1) 232 if (len >= maxsize)
506 { 233 {
507 if (maxlen <= 4) 234 if (maxsize <= 4)
508 { 235 {
509 memset (dst, '.', maxlen - 1); 236 memset (dst, '.', maxsize - 2);
510 dst [maxlen - 1] = 0; 237 dst [maxsize - 1] = 0;
511 } 238 }
512 else 239 else
513 { 240 {
514 memcpy (dst, src, maxlen - 4); 241 memcpy (dst, src, maxsize - 4);
515 memcpy (dst + maxlen - 4, "...", 4); 242 memcpy (dst + maxsize - 4, "...", 4);
516 } 243 }
244
245 len = maxsize;
517 } 246 }
518 else 247 else
519 memcpy (dst, src, len + 1); 248 memcpy (dst, src, ++len);
520}
521 249
522const std::string 250 return len;
251}
252
253char *
254vformat (const char *format, va_list ap)
255{
256 static dynbuf_text bufs[8];
257 static int bufidx;
258
259 dynbuf_text &buf = bufs [++bufidx & 7];
260
261 buf.clear ();
262 buf.vprintf (format, ap);
263 return buf;
264}
265
266char *
523format (const char *format, ...) 267format (const char *format, ...)
524{ 268{
525 int len;
526
527 {
528 char buf[128];
529
530 va_list ap; 269 va_list ap;
531 va_start (ap, format); 270 va_start (ap, format);
532 len = vsnprintf (buf, sizeof (buf), format, ap); 271 char *buf = vformat (format, ap);
533 va_end (ap); 272 va_end (ap);
534 273
535 assert (len >= 0); // shield againstz broken vsnprintf's
536
537 // was our static buffer short enough?
538 if (len < sizeof (buf))
539 return std::string (buf, len);
540 }
541
542 {
543 // longer, try harder
544 char *buf = salloc<char> (len + 1);
545
546 va_list ap;
547 va_start (ap, format);
548 vsnprintf (buf, len + 1, format, ap);
549 va_end (ap);
550
551 const std::string s (buf, len);
552 sfree<char> (buf, len + 1);
553
554 return buf; 274 return buf;
555 }
556} 275}
557 276
558tstamp now () 277tstamp
278now ()
559{ 279{
560 struct timeval tv; 280 struct timeval tv;
561 281
562 gettimeofday (&tv, 0); 282 gettimeofday (&tv, 0);
563 return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); 283 return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6);
628 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, 348 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL,
629 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, 349 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL,
630 0x2d02ef8dL 350 0x2d02ef8dL
631}; 351};
632 352
353void
354thread::start (void *(*start_routine)(void *), void *arg)
355{
356 pthread_attr_t attr;
633 357
358 pthread_attr_init (&attr);
359 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
360 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < sizeof (long) * 4096
361 ? sizeof (long) * 4096 : PTHREAD_STACK_MIN);
362#ifdef PTHREAD_SCOPE_PROCESS
363 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
364#endif
365
366 sigset_t fullsigset, oldsigset;
367 sigfillset (&fullsigset);
368
369 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
370
371 if (pthread_create (&id, &attr, start_routine, arg))
372 cleanup ("unable to create a new thread");
373
374 pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
375}
376

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines