[ENet-discuss] Calling enet_initialise() more than once

Ruud van Gaal ruud at racer.nl
Wed Jul 27 02:34:51 PDT 2011


Ross incorrectly assumed you were talking about multithreading.
If you look at the source, win32.c, you see:

---
int enet_initialize (void)
{
    WORD versionRequested = MAKEWORD (1, 1);
    WSADATA wsaData;

    if (WSAStartup (versionRequested, & wsaData))
       return -1;

    if (LOBYTE (wsaData.wVersion) != 1||
        HIBYTE (wsaData.wVersion) != 1)
    {
       WSACleanup ();

       return -1;
    }

    timeBeginPeriod (1);

    return 0;
}

void
enet_deinitialize (void)
{
    timeEndPeriod (1);

    WSACleanup ();
}
---

I'm not sure what the behavior of WSAStartup and WSACleanup() is with
respect to reference counting, but googling that will give you the answer.
I know that timeBeginPeriod() is really reference counted, but I think these
days the timer is always at 1 ms resolution, so it doesn't really harm no
matter how many calls you make.

As for a definite answer, I'd just wrap ENet's initialize and use your own
guard:

static bool setup;
void InitEnet()
{
  if(!setup)
  {
    enet_initialize();
    setup=true;
  }
}
void CloseENet()
{
  if(setup)
  { enet_deinitialize();
   setup=false;
  }
}

That will always be safe (just the ugly global var which you might tuck
away) and safeguard you from future changes in ENet wrt to the reference
counted behavior.

Cheers,
Ruud

On Wed, Jul 27, 2011 at 7:12 AM, Soren Dreijer <dreijer at echobit.net> wrote:

> I don't see how that answers the question. As the FAQ states, enet doesn't
> use any significant global variables, but it definitely uses 'some' or else
> we wouldn't be required to call a global initialize function. The question
> is whether a second call will reset anything that existing ENetHost
> structures rely on.
>
> > So my assumption would be NO, but you could try .. but why you would want
> > to call this multiple times is a mystery, maybe rethink your application
> > design ..
>
> As I said in my original post, most APIs on Windows let you call initialize
> functions multiple times in case you want to be absolutely sure something
> is
> initialized before using it. What if you happen to use two (unrelated)
> libraries in your project that both use enet internally? Unless those two
> libraries defer to the caller to initialize enet (which might not be
> intended at all if the library is supposed to hide away such details) then
> there's no way for me to guarantee that enet_initialize() won't be called
> twice in the same process.
>
> I already worked around this 'issue' in my own code, but I'm mostly just
> being proactive here and trying to determine if it'd be possible or not.
>
> > -----Original Message-----
> > From: enet-discuss-bounces at cubik.org [mailto:enet-discuss-
> > bounces at cubik.org] On Behalf Of Ross Linder
> > Sent: Tuesday, July 26, 2011 11:39 PM
> > To: Discussion of the ENet library
> > Subject: Re: [ENet-discuss] Calling enet_initialise() more than once
> >
> > I think the FAQ answers this pretty much ..
> >
> > -->
> > Is ENet thread safe?
> >
> > ENet does not use any significant global variables, the vast majority of
> state
> > is encapsulated in the ENetHost structure. As such, as long as the
> > application guards access to this structure, then ENet should operate
> fine
> in
> > a multithreaded environment.
> > <--
> >
> > So my assumption would be NO, but you could try .. but why you would want
> > to
> > call this multiple times is a mystery, maybe rethink your application
> > design ..
> > --
> >
> > On Tuesday 26 July 2011 20:09:09 Soren Dreijer wrote:
> > > Am I allowed to call enet_initialize() more than once in my
> application?
> > > I'm coming from the world of Windows where most APIs, such as
> > WSAStartup()
> > > or CoInitializeEx(), can be called multiple times as long as their
> cleanup
> > > counterparts are called the same number of times.
> > >
> > >
> > >
> > > Is that true for enet as well or will enet_initialize() reset any
> already
> > > initialized global state thereby potentially messing up other parts of
> my
> > > application that currently use enet?
> >
> >
> > _______________________________________________
> > ENet-discuss mailing list
> > ENet-discuss at cubik.org
> > http://lists.cubik.org/mailman/listinfo/enet-discuss
>
> _______________________________________________
> ENet-discuss mailing list
> ENet-discuss at cubik.org
> http://lists.cubik.org/mailman/listinfo/enet-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cubik.org/pipermail/enet-discuss/attachments/20110727/c28bf547/attachment.html>


More information about the ENet-discuss mailing list