Thread safeness of RM_StringPtrLen

16 views
Skip to first unread message

Simone Mosciatti

unread,
Dec 4, 2016, 7:18:10 AM12/4/16
to redis-module-devs
Hi again folks,

I am completely the multi threaded version of my rediSQL

However when I put the system under concurrent load some things start to cripple down.

The whole problem is related to strings. SQLite need a query in input as UTF8 (for now I am working with the UFT8 subset of ASCII).

Before starting a thread a created an object Query where I put everything that SQLite need, the relevant part is here:

Query *q = RM_Alloc(sizeof(Query))
q
->query = RM_StringPtrLen(argv[1], &(q->query_len))

In my understanding now in q->query point to a string that contains the first argument passed to the command that is being executed, and q->query_len contains the length of such string.

Doing like this, roughly 1/10 th of the time there is some nasty error related to the string itself, the string seems to terminated to early and q->query_len is different from strlen(q->query)

The solution that seems to work is to copy the string in another area of memory and work on that.

buffer_query = RedisModule_StringPtrLen(argv[1], NULL);
q
->query = RedisModule_Alloc(sizeof(char) * (strlen(buffer_query) + 1));
strcpy
(q->query, buffer_query);
q
->query_len = strlen(q->query);

Now I do not see any problem and everything works smoothly but slower.

Is this behaviour expected?


Itamar Haber

unread,
Dec 4, 2016, 7:37:52 AM12/4/16
to Simone Mosciatti, redis-module-devs
Hi Simone,

Yes, that is expected. Once your thread is created, the original function returns so all of its context is gone as well. Anything you need in the thread should be copied or you'll be referencing invalid memory locations.

--
You received this message because you are subscribed to the Google Groups "redis-module-devs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-module-devs+unsubscribe@googlegroups.com.
To post to this group, send email to redis-module-devs@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/redis-module-devs/87daf050-f018-4007-a8b3-85277cc22937%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Simone Mosciatti

unread,
Dec 4, 2016, 8:31:35 AM12/4/16
to redis-module-devs
Hi Itamar,

Perfect, great to know.

I was expecting that the Context was in the heap and that I was pointing on something that was suppose to be freed after the function reply to the client.

Thanks,

Cheers


On Sunday, December 4, 2016 at 1:37:52 PM UTC+1, Itamar Haber wrote:
Hi Simone,

Yes, that is expected. Once your thread is created, the original function returns so all of its context is gone as well. Anything you need in the thread should be copied or you'll be referencing invalid memory locations.
On Sun, Dec 4, 2016 at 2:18 PM, Simone Mosciatti <mweb...@gmail.com> wrote:
Hi again folks,

I am completely the multi threaded version of my rediSQL

However when I put the system under concurrent load some things start to cripple down.

The whole problem is related to strings. SQLite need a query in input as UTF8 (for now I am working with the UFT8 subset of ASCII).

Before starting a thread a created an object Query where I put everything that SQLite need, the relevant part is here:

Query *q = RM_Alloc(sizeof(Query))
q
->query = RM_StringPtrLen(argv[1], &(q->query_len))

In my understanding now in q->query point to a string that contains the first argument passed to the command that is being executed, and q->query_len contains the length of such string.

Doing like this, roughly 1/10 th of the time there is some nasty error related to the string itself, the string seems to terminated to early and q->query_len is different from strlen(q->query)

The solution that seems to work is to copy the string in another area of memory and work on that.

buffer_query = RedisModule_StringPtrLen(argv[1], NULL);
q
->query = RedisModule_Alloc(sizeof(char) * (strlen(buffer_query) + 1));
strcpy
(q->query, buffer_query);
q
->query_len = strlen(q->query);

Now I do not see any problem and everything works smoothly but slower.

Is this behaviour expected?


--
You received this message because you are subscribed to the Google Groups "redis-module-devs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-module-d...@googlegroups.com.
To post to this group, send email to redis-mo...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages