Differences between revisions 2 and 3
Revision 2 as of 2014-06-03 06:31:25
Size: 5891
Editor: HungTa
Comment:
Revision 3 as of 2014-06-03 06:42:01
Size: 1211
Editor: HungTa
Comment:
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
The Name Service is a client library that is linked with every component that uses it. It opens a cluster-wide, shared memory, non-persistent checkpoint using the name service's well-known [[Handle]].

The name service uses this checkpoint to resolve string names into objects.
The Thread Pool is a client library that is linked with every component that uses it.
Line 16: Line 14:
#include <clNameApi.hxx> #include <clThreadPool.hxx>
Line 18: Line 16:
// Definition of user task
typedef uint32_t (*CallbackT) (void* invocation);
Line 20: Line 20:
class NameException: public std::exception
{
  ...
};
Line 25: Line 21:
class NameRegistrar class Callable
Line 28: Line 24:

    typedef enum {
         MODE_REDUNDANCY,
         MODE_ROUND_ROBIN,
         MODE_PREFER_LOCAL/* return a handle that points to this process (if it exists)
                             if it does not exist return a handle that points to this NODE.
                             if that does not exist, return any handle
                             So choose "closer" handles over ones that are far away
                             this mode makes communications more efficient
                          */
         MODE_NO_CHANGE, /* This is not a mode; it tells the API to not change the existing mode (or use MODE_REDUNDANCY if creating) */

         } MappingMode;
    /* For more convenience and logical, should move mapping mode setting to set functions
       For more clearance on this parameter, see the comments of the functions
    */
    //void setMode(const char* name, MappingMode m);
     

    /* Associate a name with a handle and pointer and associate a handle with a pointer.
       If the name does not exist, it is created. If it exists, it is overwritten.
       If the handle format contains a node or process designator, then this mapping will be removed when the node/process fails.

       the void* object pointer is local to this process; it does not need to be part of the checkpoint.
       
       This association is valid for all SAFplus API name lookups, and for AMF entity names.
       The mapping mode parameter is mandatory and specifies what the mapping mode is, based on it, the get functions retrieves appropriate handles
     */
    void set(const char* name, SAFplus::Handle handle, MappingMode m, void* object=NULL);
    void set(const std::string& name, SAFplus::Handle handle, MappingMode m, void* object=NULL);
    

     /* Associate a name with a handle and pointer and associate a handle with a pointer (if object != NULL).
        If the name does not exist, it is created. If the name exists, this mapping is appended (the original mapping is not removed).
        If there are multiple associations, the first association will always be returned.
        If the handle format contains a node or process designator, then this mapping will be removed when the node/process fails.
        If the name has more than one mapping another mapping will become the default response for this name.

        This association is valid for all SAFplus API name lookups, and for AMF entity names.
        The mapping mode parameter is mandatory and specifies what the mapping mode is, based on it, the get functions look up appropriate handle: if it's different from MappingMode::MODE_NO_CHANGE, the mapping mode that was set for this name before will be replaced with it, otherwise (omitted), the mapping mode that was set for this name before will be kept as it was (no change)
      */
    void append(const char* name, SAFplus::Handle handle, MappingMode m, void* object=NULL);
    void append(const std::string& name, SAFplus::Handle handle, MappingMode m, void* object=NULL);
    // Associate name with arbitrary data. A copy of the data is made.
    void set(const char* name, const void* data, int length);
    void set(const std::string& name, const void* data, int length);

    // Associate name with arbitrary data. A copy of the data is NOT made; this call transfers the reference count (ownership) to the callee.
    void set(const char* name, SAFplus::Buffer*);
    void set(const std::string& name, SAFplus::Buffer*);

    // Get a handle associated with the data
    // The SAFplus APIs use these calls to resolve names to handles or objects.
    std::pair<SAFplus::Handle&,void* object> get(const char* name) throws(NameException&);
    std::pair<SAFplus::Handle&,void* object> get(const std::string& name) throws(NameException&);

    SAFplus::Handle& get(const char* name) throws(NameException&);
    SAFplus::Handle& get(const std::string& name) throws(NameException&);
 
    // Get object based on handle
    void* get(const SAFplus::Handle&) throws(NameException&);

    
    // Get a handle associated with the data
    // The SAFplus APIs use these calls to resolve names to handles or objects.
    // Do not free the returned buffer, call Buffer.decRef();
    SAFplus::Buffer& get(const char* name) throws(NameException&);
    SAFplus::Buffer& get(const std::string& name) throws(NameException&);
    run(CallbackT* fn)
Line 100: Line 29:
// Name is a singleton class; only one per process.
extern NameRegistrar name;

The Thread Pool

Thread Pools are useful when you need to limit the number of threads running in your application at the same time. There is a performance overhead associated with starting a new thread, and each thread is also allocated some memory for its stack etc.

Instead of starting a new thread for every task to execute concurrently, the task can be passed to a thread pool. As soon as the pool has any idle threads the task is assigned to one of them and executed. Internally, the thread pool handle has to be created first, then the tasks are inserted into this handle and executed.

Thread pools are often used in multi threaded servers. Each connection arriving at the server via the network is wrapped as a task and passed on to a thread pool. The threads in the thread pool will process the requests on the connections concurrently.

Implementation

The Thread Pool is a client library that is linked with every component that uses it.

#include <clThreadPool.hxx>

APIs

// Definition of user task typedef uint32_t (*CallbackT) (void* invocation);

   1 class Callable
   2   {
   3   public:
   4     run(CallbackT* fn)
   5 
   6     
   7   };

SAFplus: ThreadPool (last edited 2014-06-04 18:42:59 by AndrewStone)