~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/cslib/CSThread.cc

  • Committer: Lee Bieber
  • Date: 2010-10-22 16:47:38 UTC
  • mfrom: (1841.1.7 drizzle_pbms)
  • Revision ID: kalebral@gmail.com-20101022164738-vv8w22b8towpb307
Merge Barry - fix bug 657830: PBMS build failure in GCC 4.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 
31
31
#ifdef OS_WINDOWS
32
32
#include <signal.h>
33
 
#include "uniwin.h"
 
33
//#include "uniwin.h"
 
34
#define SIGUSR1 30
 
35
#define SIGUSR2 31
 
36
 
34
37
#else
35
38
#include <signal.h>
36
39
#include <sys/signal.h>
37
 
#endif
38
40
#include <unistd.h>
 
41
#endif
39
42
#include <errno.h>
40
43
 
41
44
#include "CSGlobal.h"
208
211
void CSThread::removeFromList()
209
212
{
210
213
        if (myThreadList && isRunning) {
 
214
                CSThread *myself = this; // pop_() wants to take a reference to its parameter.
211
215
                enter_();
212
216
                /* Retain the thread in order to ensure
213
217
                 * that after it is removed from the list,
215
219
                 * unlock_() call invalid, because it requires
216
220
                 * on the thread.
217
221
                 */
218
 
                push_(this);
 
222
                push_(myself);
219
223
                lock_(myThreadList);
220
 
                myThreadList->remove(RETAIN(this));
 
224
                myThreadList->remove(RETAIN(myself));
221
225
                unlock_(myThreadList);
222
 
                pop_(this);
 
226
                pop_(myself);
223
227
                outer_();
224
228
        }
225
229
        this->release();
288
292
        return NULL;
289
293
}
290
294
 
291
 
void CSThread::start()
 
295
void CSThread::start(bool detached)
292
296
{
293
297
        int err;
294
298
 
303
307
                        break;
304
308
                usleep(10);
305
309
        }
 
310
        
 
311
        isDetached = detached;
 
312
        if (detached)
 
313
                pthread_detach(iThread);
306
314
}
307
315
 
308
316
void CSThread::stop()
313
321
 
314
322
void *CSThread::join()
315
323
{
316
 
        void    *return_data;
 
324
        void    *return_data = NULL;
317
325
        int             err;
318
326
 
319
327
        enter_();
320
 
        if ((err = pthread_join(iThread, &return_data)))
 
328
        if (isDetached) {
 
329
                while (isRunning && !pthread_kill(iThread, 0)) 
 
330
                        usleep(100);
 
331
        } else if ((err = pthread_join(iThread, &return_data))) {
321
332
                CSException::throwOSError(CS_CONTEXT, err);
 
333
        }
 
334
 
322
335
        return_(return_data);
323
336
}
324
337
 
334
347
 
335
348
void CSThread::signal(unsigned int sig)
336
349
{
 
350
#ifndef OS_WINDOWS // Currently you cannot signal threads on windows.
337
351
        int err;
338
352
 
339
353
        setSignalPending(sig);
343
357
                if (err != ESRCH) /* No such process */
344
358
                        CSException::throwOSError(CS_CONTEXT, err);
345
359
        }
 
360
#endif
346
361
}
347
362
 
348
363
void CSThread::throwSignal()
389
404
                                if (relTop->x.r_pooled)
390
405
                                        relTop->x.r_pooled->returnToPool();
391
406
                                break;
 
407
                        case CS_RELEASE_MEM:
 
408
                                if (relTop->x.r_mem)
 
409
                                        cs_free(relTop->x.r_mem);
 
410
                                break;
 
411
                        case CS_RELEASE_OBJECT_PTR:
 
412
                                if ((relTop->x.r_objectPtr) && (obj = *(relTop->x.r_objectPtr)))
 
413
                                        obj->release();
 
414
                                break;
392
415
                }
393
416
        }
394
417
}
463
486
        isUp = false;
464
487
        if ((err = pthread_key_create(&sThreadKey, NULL))) {
465
488
                CSException::logOSError(CS_CONTEXT, errno);
466
 
                return false;
467
489
        } else
468
490
                isUp = true;
469
491
                
512
534
                return (CSThread*) NULL;
513
535
                
514
536
#ifdef DEBUG
515
 
        if (self->iRefCount == 0) {
 
537
        /* PMC - Problem is, if this is called when releasing a
 
538
         * thread, then we have the reference count equal to
 
539
         * zero.
 
540
        if (self && !self->iRefCount) {
516
541
                pthread_setspecific(sThreadKey, NULL);
517
 
                CSException::throwAssertion(CS_CONTEXT, "Bad self pointer.");
518
 
        }       
 
542
                CSException::throwAssertion(CS_CONTEXT, "Freed self pointer referenced.");
 
543
        }
 
544
        */
519
545
#endif
520
546
 
521
547
        return self;
614
640
{
615
641
}
616
642
 
617
 
void *CSDaemon::run()
 
643
void CSDaemon::try_Run(CSThread *self, const bool c_must_sleep)
618
644
{
619
 
        bool must_sleep = false;
620
 
 
621
 
        CLOBBER_PROTECT(must_sleep);
622
 
 
623
 
        enter_();
624
 
        CLOBBER_PROTECT(self);
625
 
 
626
 
        myMustQuit = !initializeWork();
627
 
 
628
 
        restart:
629
645
        try_(a) {
 
646
                 bool must_sleep = c_must_sleep; // This done to avoid longjmp() clobber.
630
647
                while (!myMustQuit) {
631
648
                        if (must_sleep) {
632
649
                                lock_(this);
646
663
                        myMustQuit = true;
647
664
        }
648
665
        cont_(a);
649
 
        if (!myMustQuit) {
 
666
}
 
667
 
 
668
void *CSDaemon::run()
 
669
{
 
670
        bool must_sleep = false;
 
671
 
 
672
        enter_();
 
673
 
 
674
        myMustQuit = !initializeWork();
 
675
 
 
676
        while  (!myMustQuit) {
 
677
                try_Run(self, must_sleep);
650
678
                must_sleep = true;
651
 
                goto restart;
652
679
        }
653
680
 
654
681
        /* Prevent signals from going off in completeWork! */