git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* static initializers
@ 2014-01-06 22:02 Stefan Zager
       [not found] ` <CAHOQ7J-UKvG2j2PZJqhJP=5YLniHpELtOTzF5i1WsiGkhzZHhw@mail.gmail.com>
  0 siblings, 1 reply; 2+ messages in thread
From: Stefan Zager @ 2014-01-06 22:02 UTC (permalink / raw)
  To: git

Howdy,

Is there any policy on making static initializers thread-safe?  I'm
working on an experimental patch to introduce threading, but I'm
running into a few non-thread-safe bits like this, in convert.c:

static const char *conv_attr_name[] = {
    "crlf", "ident", "filter", "eol", "text",
};
#define NUM_CONV_ATTRS ARRAY_SIZE(conv_attr_name)

static void convert_attrs(struct conv_attrs *ca, const char *path)
{
    int i;
    static struct git_attr_check ccheck[NUM_CONV_ATTRS];

    if (!ccheck[0].attr) {
        for (i = 0; i < NUM_CONV_ATTRS; i++)
            ccheck[i].attr = git_attr(conv_attr_name[i]);
        user_convert_tail = &user_convert;
        git_config(read_convert_config, NULL);
    }
}



The easy fix would be to stick the initialization bit into an 'extern
int init_convert_config();' function, and invoke it prior to the
threaded part of my code.  That would be no worse than the current
state of affairs, which is to say that adding threading is rife with
hidden perils.

A more comprehensive fix might be:

#include <pthread.h>

static pthread_mutex_t convert_config_mutex = PTHREAD_MUTEX_INITIALIZER;

static void convert_attrs(struct conv_attrs *ca, const char *path)
{
    int i;
    static struct git_attr_check ccheck[NUM_CONV_ATTRS];

    pthread_mutex_lock(&convert_config_mutex);
    if (!ccheck[0].attr) {
        for (i = 0; i < NUM_CONV_ATTRS; i++)
            ccheck[i].attr = git_attr(conv_attr_name[i]);
        user_convert_tail = &user_convert;
        git_config(read_convert_config, NULL);
    }
    pthread_mutex_unlock(&convert_config_mutex);
}


Unfortunately, I don't think mingw/msys supports
PTHREAD_MUTEX_INITIALIZER.  A possible workaround would be:

static pthread_mutex_t convert_config_mutex;

static void init_convert_config_mutex() __attribute__((constructor));
static void init_convert_config_mutex()
{
    pthread_mutex_init(&convert_config_mutex);
}


But then, I'm not whether mingw/msys supports __attribute__(constructor).


Can anyone give me some guidance before I go to much further into the
weeds (and I'm neck-deep as it is)?

Thanks,

Stefan

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Fwd: static initializers
       [not found] ` <CAHOQ7J-UKvG2j2PZJqhJP=5YLniHpELtOTzF5i1WsiGkhzZHhw@mail.gmail.com>
@ 2014-01-07  0:19   ` Erik Faye-Lund
  0 siblings, 0 replies; 2+ messages in thread
From: Erik Faye-Lund @ 2014-01-07  0:19 UTC (permalink / raw)
  To: Stefan Zager, GIT Mailing-list; +Cc: msysGit

On Mon, Jan 6, 2014 at 11:05 PM, Stefan Zager <szager@google.com> wrote:
> Forwarding to msysgit for any guidance about win equivalents for
> PTHREAD_MUTEX_INITIALIZER or __attribute__((constructor)).

As you've probably already guessed, PTHREAD_MUTEX_INITIALIZER isn't
supported in our pthreads-emulator. I did send out a patch adding
support for it a while ago, but it hasn't been heavily tested.

__attribute__((constructor)) doesn't work on MSVC, which we also build with.

-- 
-- 
*** Please reply-to-all at all times ***
*** (do not pretend to know who is subscribed and who is not) ***
*** Please avoid top-posting. ***
The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free.

You received this message because you are subscribed to the Google
Groups "msysGit" group.
To post to this group, send email to msysgit@googlegroups.com
To unsubscribe from this group, send email to
msysgit+unsubscribe@googlegroups.com
For more options, and view previous threads, visit this group at
http://groups.google.com/group/msysgit?hl=en_US?hl=en

--- 
You received this message because you are subscribed to the Google Groups "msysGit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to msysgit+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-01-07  0:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-01-06 22:02 static initializers Stefan Zager
     [not found] ` <CAHOQ7J-UKvG2j2PZJqhJP=5YLniHpELtOTzF5i1WsiGkhzZHhw@mail.gmail.com>
2014-01-07  0:19   ` Fwd: " Erik Faye-Lund

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).