Hi all,
i am trying to implement a multi-threaded resource manager. i followed the example program in the Momentics help, but i didn’t get what i expected.
Here is my code of the resource manager initialization:
int ResMgrThread(void* args)
{
/* declare variables we'll be using */
thread_pool_attr_t pool_attr;
resmgr_attr_t resmgr_attr;
dispatch_t *dpp;
thread_pool_t *tpp;
int id;
/* initialize dispatch interface */
if((dpp = dispatch_create()) == NULL)
{
return EXIT_FAILURE;
}
/* initialize resource manager attributes */
memset(&resmgr_attr, 0, sizeof(resmgr_attr));
resmgr_attr.nparts_max = 1;
resmgr_attr.msg_max_size = 2048;
/* initialize functions for handling messages */
iofunc_func_init(_RESMGR_CONNECT_NFUNCS, &connect_funcs,
_RESMGR_IO_NFUNCS, &io_funcs);
connect_funcs.open = io_open;
io_funcs.devctl = io_devctl;
io_funcs.close_dup = io_close;
/* initialize resource manager attributes */
memset(&resmgr_attr, 0, sizeof(resmgr_attr));
resmgr_attr.nparts_max = 1;
resmgr_attr.msg_max_size = 2048;
/* initialize device attributes */
iofunc_attr_init(&(deviceattr.attr), S_IFNAM | 0666, 0, 0);
deviceattr.attr.mount = &mountpoint;
/* attach device name */
id = resmgr_attach(
dpp, /* dispatch handle */
&resmgr_attr, /* resource manager attrs */
"/dev/my_resmgr", /* device name */
_FTYPE_ANY, /* open type */
0, /* flags */
&connect_funcs, /* connect routines */
&io_funcs, /* I/O routines */
&deviceattr); /* handle */
if(id == -1)
{
return EXIT_FAILURE;
}
/* initialize thread pool attributes */
memset(&pool_attr, 0, sizeof pool_attr);
pool_attr.handle = dpp;
pool_attr.context_alloc = dispatch_context_alloc;
pool_attr.block_func = dispatch_block;
pool_attr.unblock_func = dispatch_unblock;
pool_attr.handler_func = dispatch_handler;
pool_attr.context_free = dispatch_context_free;
pool_attr.lo_water = 8;
pool_attr.hi_water = 12;
pool_attr.increment = 1;
pool_attr.maximum = 16;
/* allocate a thread pool handle */
if((tpp = thread_pool_create(&pool_attr,
POOL_FLAG_EXIT_SELF)) == NULL)
{
return EXIT_FAILURE;
}
/* start the threads, will not return */
thread_pool_start(tpp);
return EOK;
}
And this is the devctl handler function of the resource manager:
int io_devctl( resmgr_context_t *ctp, io_devctl_t *msg, struct ocb *ocb)
{
int i_ret, i_reqLen, i_respLen;
void* pv_data;
int cmd;
/* number of bytes received */
i_reqLen = (uint16_t) msg->i.nbytes;
/* execute devctl handler default function */
i_ret = iofunc_devctl_default(ctp, msg, &(ocb->hdr));
if(i_ret != _RESMGR_DEFAULT)
{
return i_ret;
}
/* get pointer to the data passed by client library */
pv_data = (void*) _DEVCTL_DATA(msg->i);
/* get command */
cmd = (int)msg->i.dcmd;
/* distinguish the given command */
switch(cmd)
{
/* command 1 */
case CMD_1:
{
i_ret = proc_cmd1(pv_data, i_reqLen, &i_respLen);
if(i_ret == EOK)
{
/* copy client ID to Open Control Block */
ocb->client_id = ((Cmd1_Data*) pv_data)->client_id;
if(ocb->client_id == 0)
{
printf("Debug: Sleeping 20s for Client ID=0");
sleep(20);
}
}
else
{
PRINTD("Error Processing FEA_OPEN_SESSION_CMD");
}
break;
}
default:
{
return (ENOSYS);
}
}
/* return with an error */
if(i_ret != EOK)
{
return i_ret;
}
return (_RESMGR_PTR(ctp, &msg->o, sizeof(msg->o) + i_respLen));
}
the function proc_cmd1() basically write a new unique client ID if it returns successfully.
so i did a small test by implementing 2 small test client program. Both clients send requests with the CMD_1 command using devctl to the resource manager.
The first client receives client ID=0, and the second client receives client ID=1.
In case of client 1, i expect that the handler will execute the 20 seconds sleep function, since client ID=0 is returned from the proc_cmd1() function. While the handler is sleeping due to client 1 request, i tried to run client 2.
I expected - since the resource manager is supposed to be multi-threading - that client 2 request is processed by another thread of the resource manager. But unfortunatelly it is not. Request from client 2 seems to be also blocked while resource manager is sleeping due to request client 1.
Am i doing something wrong here?
thanks for any answer.