You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modules-dev@httpd.apache.org by Bob Weeks <bo...@btinternet.com.INVALID> on 2023/03/23 10:48:29 UTC

Shared memory - data between threads

Hello,

A few years ago I ported a Netscape NSAPI module to Apache 1.3 for a 
financial institution which assisted in processing financial 
transactions. This module created a sub-pool which contained a number of 
linked lists with session data created in the module initializer part of 
the module MODULE_VAR_EXPORT structure. All the threads of the Apache 
process could see the data and process information in the 'handler' part 
of the code.


in my new module I am using the ap_hook_post_config hook for pre-forking 
configuration and this:
Creates a Sub-pool
Connected to a mysql database
Creates a structure block in this new sub-pool from the data in the 
returned data.

In my module, instead of a handler I am using the ap_hook_check_access 
hook to verify the data as all I want to do is return a OK or Unauthorised

However in one condition I need to reset data in the database with new 
data. However, some threads do not see this change even thought is a 
shared memory pool.

I created a little test module to just increment a value in this code 
block each time I called the page and printout the next time it passed.

         ap_log_error(APLOG_MARK, APLOG_INFO,0,r->server,"Config = 
%ld",(long)cfg->Data);
         cfg->Data++;
         return OK;


[Thu Mar 23 09:34:22.779569 2023] [btest:info] [pid 26649] Config = 4
[Thu Mar 23 09:34:40.193054 2023] [btest:info] [pid 26647] Config = 5
[Thu Mar 23 09:34:40.193422 2023] [btest:info] [pid 26647] Config = 6
[Thu Mar 23 09:34:40.203726 2023] [btest:info] [pid 26647] Config = 7
[Thu Mar 23 09:34:40.206444 2023] [btest:info] [pid 26662] Config = 9
[Thu Mar 23 09:34:40.206663 2023] [btest:info] [pid 26651] Config = 27

Thread 26662 and 26651 still had inconsistent data.

Is there a way of sharing data between threads with out creating a 
semaphore


Appoligies if this has been asked before.


Bob


Re: Shared memory - data between threads

Posted by Yann Ylavic <yl...@gmail.com>.
Hello;

On Thu, Mar 23, 2023 at 11:48 AM Bob Weeks
<bo...@btinternet.com.invalid> wrote:
>
> I created a little test module to just increment a value in this code
> block each time I called the page and printout the next time it passed.
>
>          ap_log_error(APLOG_MARK, APLOG_INFO,0,r->server,"Config =
> %ld",(long)cfg->Data);
>          cfg->Data++;
>          return OK;
>
>
> [Thu Mar 23 09:34:22.779569 2023] [btest:info] [pid 26649] Config = 4
> [Thu Mar 23 09:34:40.193054 2023] [btest:info] [pid 26647] Config = 5
> [Thu Mar 23 09:34:40.193422 2023] [btest:info] [pid 26647] Config = 6
> [Thu Mar 23 09:34:40.203726 2023] [btest:info] [pid 26647] Config = 7
> [Thu Mar 23 09:34:40.206444 2023] [btest:info] [pid 26662] Config = 9
> [Thu Mar 23 09:34:40.206663 2023] [btest:info] [pid 26651] Config = 27
>
> Thread 26662 and 26651 still had inconsistent data.
>
> Is there a way of sharing data between threads with out creating a
> semaphore

It depends on the size of your data, if it fits in a long as in your
example you can use the apr_atomic API (32 or 64 bits).
So something like apr_atomic_inc32(&cfg->Data) or
apr_atomic_inc64(&cfg->Data) depending on whether the Data is an
apr_uint32_t or an apr_uint64_t respectively.
If your data is above 64bit, you need a mutex (apr_global_mutex_t for
a memory shared between processes or apr_thread_mutex_t it's shared
between threads in the same process).

In any case if multiple readers/writers access your Data concurrently,
the access should be atomic (be it with CPU atomic primitives for
sizeof(Data) <= 8, or with a mutex exclusive section for larger
structs).


Regards;
Yann.