ARPDAS_QNX6 1.0
|
00001 #include <errno.h> 00002 #include "DG_Resmgr.h" 00003 #include "nortlib.h" 00004 #include "nl_assert.h" 00005 00006 /** DG_Resmgr.h Framework for 00007 */ 00008 00009 DG_dispatch_client::DG_dispatch_client() { 00010 dispatch = NULL; 00011 } 00012 00013 DG_dispatch_client::~DG_dispatch_client() { 00014 if ( dispatch != NULL ) 00015 detach(); 00016 } 00017 00018 void DG_dispatch_client::attach(DG_dispatch *disp) { 00019 // assert( disp != NULL ); 00020 dispatch = disp; 00021 dispatch->client_add(this); 00022 } 00023 00024 void DG_dispatch_client::detach() { 00025 //assert(dispatch != NULL); 00026 dispatch->client_rm(this); 00027 dispatch = NULL; 00028 } 00029 00030 DG_dispatch::DG_dispatch() { 00031 dpp = dispatch_create(); 00032 if ( dpp == NULL ) 00033 nl_error( 3, "Failed to allocate dispatch handle." ); 00034 quit_received = 0; 00035 } 00036 00037 void DG_dispatch::ready_to_quit() { 00038 quit_received = 1; 00039 } 00040 00041 DG_dispatch::~DG_dispatch() { 00042 if ( single_ctp != NULL ) 00043 dispatch_context_free(single_ctp); 00044 dispatch_destroy(dpp); 00045 } 00046 00047 void DG_dispatch::Loop() { 00048 single_ctp = dispatch_context_alloc(dpp); 00049 if ( single_ctp == NULL ) 00050 nl_error(3, "dispatch_context_alloc failed: errno %d", errno ); 00051 dispatch_context_t *ctp = single_ctp; 00052 while (1) { 00053 ctp = dispatch_block(ctp); 00054 if ( ctp == NULL ) 00055 nl_error( 3, "Block error: %d", errno ); 00056 dispatch_handler(ctp); 00057 if ( quit_received && ctp->resmgr_context.rcvid == 0 00058 && all_closed() ) 00059 break; 00060 } 00061 } 00062 00063 int DG_dispatch::all_closed() { 00064 int ready = 1; 00065 int not_ready = 0; 00066 std::list<DG_dispatch_client *>::iterator pos; 00067 for ( pos = clients.begin(); pos != clients.end(); ) { 00068 if ( (*pos)->ready_to_quit() ) clients.remove(*pos++); 00069 else { 00070 ready = 0; 00071 not_ready++; 00072 ++pos; 00073 } 00074 } 00075 if ( not_ready ) nl_error( -2, "Waiting on %d clients", not_ready ); 00076 return ready; 00077 } 00078 00079 void DG_dispatch::client_add(DG_dispatch_client *clt) { 00080 //assert(clt != NULL); 00081 clients.push_back(clt); 00082 } 00083 00084 void DG_dispatch::client_rm(DG_dispatch_client *clt) { 00085 //assert(clt != NULL); 00086 clients.remove(clt); 00087 }