nsprpub/pr/src/cplus/rcthread.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 /* RCThread.h */
     8 #if defined(_RCTHREAD_H)
     9 #else
    10 #define _RCTHREAD_H
    12 #include "rcbase.h"
    14 #include <prthread.h>
    16 class RCInterval;
    18 class PR_IMPLEMENT(RCThreadPrivateData)
    19 {
    20 public:
    21     RCThreadPrivateData();
    22     RCThreadPrivateData(const RCThreadPrivateData&);
    24     virtual ~RCThreadPrivateData();
    26     virtual void Release() = 0;
    28 };  /* RCThreadPrivateData */
    30 class PR_IMPLEMENT(RCThread): public RCBase
    31 {
    32 public:
    34     typedef enum 
    35     {
    36         local = PR_LOCAL_THREAD, global = PR_GLOBAL_THREAD
    37     } Scope;
    39     typedef enum
    40     {
    41         joinable = PR_JOINABLE_THREAD, unjoinable = PR_UNJOINABLE_THREAD
    42     } State;
    44     typedef enum
    45     {
    46         first = PR_PRIORITY_FIRST,
    47         low = PR_PRIORITY_LOW,
    48         normal = PR_PRIORITY_NORMAL,
    49         high = PR_PRIORITY_HIGH,
    50         urgent = PR_PRIORITY_URGENT,
    51         last = PR_PRIORITY_LAST
    52     } Priority;
    54     /*
    55      * Create a new thread, providing scope and joinability state.
    56      */
    57     RCThread(Scope scope, State state, PRUint32 stackSize=0);
    59     /*
    60      * New threads are created in a suspended state. It must be 'started"
    61      * before it begins execution in the class' defined 'RootFunction()'.
    62      */
    63     virtual PRStatus Start();
    65     /*
    66      * If a thread is created joinable, then the thread's object exists
    67      * until join is called. The thread that calls join will block until
    68      * the target thread returns from it's root function.
    69      */
    70     virtual PRStatus Join();
    72     /*
    73      * The priority of a newly created thread is the same as the creator.
    74      * The priority may be changed either by the new thread itself, by
    75      * the creator or any other arbitrary thread.
    76      */   
    77     virtual void SetPriority(Priority newPriority);
    80     /*
    81      * Interrupt another thread, causing it to stop what it
    82      * is doing and return with a well known error code.
    83      */
    84     virtual PRStatus Interrupt();
    86     /*
    87      * And in case a thread was interrupted and didn't get a chance
    88      * to have the notification delivered, a way to cancel the pending
    89      * status.
    90      */
    91     static void ClearInterrupt();
    93     /*
    94      * Methods to discover the attributes of an existing thread.
    95      */
    96     static PRThread *Self();
    97     Scope GetScope() const;
    98     State GetState() const;
    99     Priority GetPriority() const;
   101     /*
   102      * Thread private data
   103      */
   104     static PRStatus NewPrivateIndex(PRUintn* index);
   106     /*
   107      * Getting it - if you want to modify, make a copy
   108      */
   109     static RCThreadPrivateData* GetPrivateData(PRUintn index);
   111     /*
   112      * Setting it to <empty> - deletes existing data
   113      */
   114     static PRStatus SetPrivateData(PRUintn index);
   116     /*
   117      * Setting it - runtime will make a copy, freeing old iff necessary
   118      */
   119     static PRStatus SetPrivateData(PRUintn index, RCThreadPrivateData* data);
   121     /*
   122      * Scheduling control
   123      */
   124     static PRStatus Sleep(const RCInterval& ticks);
   126     friend void nas_Root(void*);
   127     friend class RCPrimordialThread;
   128 protected:
   130     /*
   131      * The instantiator of a class must not call the destructor. The base
   132      * implementation of Join will, and if the thread is created unjoinable,
   133      * then the code that called the RootFunction will call the desctructor.
   134      */
   135     virtual ~RCThread();
   137 private:
   139     /*
   140      * This is where a newly created thread begins execution. Returning
   141      * from this function is equivalent to terminating the thread.
   142      */
   143     virtual void RootFunction() = 0;
   145     PRThread *identity;
   147     /* Threads are unstarted until started - pretty startling */
   148     enum {ex_unstarted, ex_started} execution;
   150     /* There is no public default constructor or copy constructor */
   151     RCThread();
   152     RCThread(const RCThread&);
   154     /* And there is no assignment operator */
   155     void operator=(const RCThread&);
   157 public:
   158     static RCPrimordialThread *WrapPrimordialThread();    
   160  };
   162 /*
   163 ** class RCPrimordialThread
   164 */
   165 class PR_IMPLEMENT(RCPrimordialThread): public RCThread
   166 {
   167 public:
   168     /*
   169     ** The primordial thread can (optionally) wait for all created
   170     ** threads to terminate before allowing the process to exit.
   171     ** Not calling Cleanup() before returning from main() will cause
   172     ** the immediate termination of the entire process, including
   173     ** any running threads.
   174     */
   175     static PRStatus Cleanup();
   177     /*
   178     ** Only the primordial thread is allowed to adjust the number of
   179     ** virtual processors of the runtime. It's a lame security thing.
   180     */
   181     static PRStatus SetVirtualProcessors(PRIntn count=10);
   183 friend class RCThread;
   184 private:
   185     /*
   186     ** None other than the runtime can create of destruct
   187     ** a primordial thread. It is fabricated by the runtime
   188     ** to wrap the thread that initiated the application.
   189     */
   190     RCPrimordialThread();
   191     ~RCPrimordialThread();
   192     void RootFunction();
   193 };  /* RCPrimordialThread */
   195  #endif /* defined(_RCTHREAD_H) */

mercurial