{"thread":{"id":"35618","subject":"static initializers","startedAt":"2014-01-06T22:02:43Z","lastAt":"2014-01-07T00:19:32Z","messageCount":2,"participants":["Stefan Zager","Erik Faye-Lund"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"232773","messageId":"CAHOQ7J8vQ-t1knSiWQ+RKv3QvwW2DZBy29DBhkxu7tEeXoJ-CA@mail.gmail.com","threadId":"35618","inReplyTo":null,"subject":"static initializers","fromName":"Stefan Zager","fromEmail":"szager@google.com","sentAt":"2014-01-06T22:02:43Z","receivedAt":"2014-01-06T22:02:43Z","isPatch":false,"sender":{"key":"szager@google.com","avatar":null},"body":"Howdy,\n\nIs there any policy on making static initializers thread-safe?  I'm\nworking on an experimental patch to introduce threading, but I'm\nrunning into a few non-thread-safe bits like this, in convert.c:\n\nstatic const char *conv_attr_name[] = {\n    \"crlf\", \"ident\", \"filter\", \"eol\", \"text\",\n};\n#define NUM_CONV_ATTRS ARRAY_SIZE(conv_attr_name)\n\nstatic void convert_attrs(struct conv_attrs *ca, const char *path)\n{\n    int i;\n    static struct git_attr_check ccheck[NUM_CONV_ATTRS];\n\n    if (!ccheck[0].attr) {\n        for (i = 0; i < NUM_CONV_ATTRS; i++)\n            ccheck[i].attr = git_attr(conv_attr_name[i]);\n        user_convert_tail = &user_convert;\n        git_config(read_convert_config, NULL);\n    }\n}\n\n\n\nThe easy fix would be to stick the initialization bit into an 'extern\nint init_convert_config();' function, and invoke it prior to the\nthreaded part of my code.  That would be no worse than the current\nstate of affairs, which is to say that adding threading is rife with\nhidden perils.\n\nA more comprehensive fix might be:\n\n#include <pthread.h>\n\nstatic pthread_mutex_t convert_config_mutex = PTHREAD_MUTEX_INITIALIZER;\n\nstatic void convert_attrs(struct conv_attrs *ca, const char *path)\n{\n    int i;\n    static struct git_attr_check ccheck[NUM_CONV_ATTRS];\n\n    pthread_mutex_lock(&convert_config_mutex);\n    if (!ccheck[0].attr) {\n        for (i = 0; i < NUM_CONV_ATTRS; i++)\n            ccheck[i].attr = git_attr(conv_attr_name[i]);\n        user_convert_tail = &user_convert;\n        git_config(read_convert_config, NULL);\n    }\n    pthread_mutex_unlock(&convert_config_mutex);\n}\n\n\nUnfortunately, I don't think mingw/msys supports\nPTHREAD_MUTEX_INITIALIZER.  A possible workaround would be:\n\nstatic pthread_mutex_t convert_config_mutex;\n\nstatic void init_convert_config_mutex() __attribute__((constructor));\nstatic void init_convert_config_mutex()\n{\n    pthread_mutex_init(&convert_config_mutex);\n}\n\n\nBut then, I'm not whether mingw/msys supports __attribute__(constructor).\n\n\nCan anyone give me some guidance before I go to much further into the\nweeds (and I'm neck-deep as it is)?\n\nThanks,\n\nStefan\n"},{"id":"232790","messageId":"CABPQNSY+xivBoARCP4UbugQk_C4Uv9hWH2_K7AJw_uor0K33Cg@mail.gmail.com","threadId":"35618","inReplyTo":"CAHOQ7J-UKvG2j2PZJqhJP=5YLniHpELtOTzF5i1WsiGkhzZHhw@mail.gmail.com","subject":"Re: Fwd: static initializers","fromName":"Erik Faye-Lund","fromEmail":"kusmabite@gmail.com","sentAt":"2014-01-07T00:19:32Z","receivedAt":"2014-01-07T00:19:32Z","isPatch":false,"sender":{"key":"kusmabite@gmail.com","avatar":"https://avatars.githubusercontent.com/u/47073?v=4"},"body":"On Mon, Jan 6, 2014 at 11:05 PM, Stefan Zager <szager@google.com> wrote:\n> Forwarding to msysgit for any guidance about win equivalents for\n> PTHREAD_MUTEX_INITIALIZER or __attribute__((constructor)).\n\nAs you've probably already guessed, PTHREAD_MUTEX_INITIALIZER isn't\nsupported in our pthreads-emulator. I did send out a patch adding\nsupport for it a while ago, but it hasn't been heavily tested.\n\n__attribute__((constructor)) doesn't work on MSVC, which we also build with.\n\n-- \n-- \n*** Please reply-to-all at all times ***\n*** (do not pretend to know who is subscribed and who is not) ***\n*** Please avoid top-posting. ***\nThe msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free.\n\nYou received this message because you are subscribed to the Google\nGroups \"msysGit\" group.\nTo post to this group, send email to msysgit@googlegroups.com\nTo unsubscribe from this group, send email to\nmsysgit+unsubscribe@googlegroups.com\nFor more options, and view previous threads, visit this group at\nhttp://groups.google.com/group/msysgit?hl=en_US?hl=en\n\n--- \nYou received this message because you are subscribed to the Google Groups \"msysGit\" group.\nTo unsubscribe from this group and stop receiving emails from it, send an email to msysgit+unsubscribe@googlegroups.com.\nFor more options, visit https://groups.google.com/groups/opt_out.\n"}]}