8000 Cannot construct an object as a static variable in a function · Issue #207 · rsta2/circle · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Cannot construct an object as a static variable in a function #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
leiradel opened this issue Mar 4, 2021 · 7 comments
Closed

Cannot construct an object as a static variable in a function #207

leiradel opened this issue Mar 4, 2021 · 7 comments

Comments

@leiradel
Copy link
leiradel commented Mar 4, 2021

I have a function to create a singleton of a class, but the linker complains about some missing functions.

lrcpp::Frontend& lrcpp::Frontend::getInstance() {
    static uint8_t fsmMemory[sizeof(CoreFsm)] alignas(CoreFsm);
    static Frontend singleton(fsmMemory);

    return singleton;
}
$ make clean && make
  CLEAN  /home/leiradel/Develop/circle/sample/libretro
  CPP   frontend/Main.o
  CPP   frontend/Kernel.o
  CPP   frontend/Frontend.o
  CPP   frontend/Fifo.o
  CC    core/main.o
  CPP   lrcpp/src/Components.o
  CPP   lrcpp/src/Core.o
  CPP   lrcpp/src/CoreFsm.o
  CPP   lrcpp/src/Frontend.o
  LD    kernel8.elf
aarch64-linux-gnu-ld: lrcpp/src/Frontend.o: in function `lrcpp::Frontend::getInstance()':
/home/leiradel/Develop/circle/sample/libretro/lrcpp/src/Frontend.cpp:65: undefined reference to `__cxa_guard_acquire'
aarch64-linux-gnu-ld: /home/leiradel/Develop/circle/sample/libretro/lrcpp/src/Frontend.cpp:65: undefined reference to `__cxa_guard_release'
make: *** [../../Rules.mk:155: kernel8.img] Error 1

These functions prevent the static variables from being initialized more than once should two threads try to get the singleton at the same time. I'm using an #idef to special case it when building with Circle, but I think it would be better if Circle supported it directly.

@rsta2
Copy link
Owner
rsta2 commented Mar 4, 2021

This is supported too, if you use the circle-stdlib project. In Circle itself you can add the following to your project Makefile before the include statement for Rules.mk to suppress the errors:

CPPFLAGS += -fno-threadsafe-statics

Because Circle doesn't have a preemptive scheduler, this should work.

@leiradel
Copy link
Author
leiradel commented Mar 4, 2021

Because Circle doesn't have a preemptive scheduler, this should work.

But it's a problem if you use multiple cores.

This is supported too, if you use the circle-stdlib project.

It seems I really need to integrate circle-stdlib into my project :)

@rsta2
Copy link
Owner
rsta2 commented Mar 5, 2021

Because Circle doesn't have a preemptive scheduler, this should work.

But it's a problem if you use multiple cores.

That's true. Unfortunately the precise semantics of __cxa_guard_*() is currently not that clear to me, so that I can implement it in Circle. If is is possible to implement it, it will take longer in any way.

@leiradel
Copy link
Author
leiradel commented Mar 5, 2021

I understand. There's a open source implementation (cxa_guard.cxx) that maybe could be used, but that's not critical and entire up to you. The license is this one I believe.

For now I just have the #ifdef I mentioned above and it's working just fine. Feel free to close this issue if you're not planing on implementing this in Circle itself, and thanks for pointing me to circle-stdlib.

@rsta2
Copy link
Owner
rsta2 commented Mar 6, 2021

Thanks for the link! I will see, what I can do.

rsta2 added a commit that referenced this issue Mar 21, 2021
Requires the implementation of the functions __cxa_guard_acquire()
and __cxa_guard_release(), which will be automatically used by the
compiler.

Issue #207
@rsta2
Copy link
Owner
rsta2 commented Mar 21, 2021

This feature has been implemented on the develop branch now.

@rsta2
Copy link
Owner
rsta2 commented May 14, 2021

This feature has been implemented in Circle 44. So this can be closed.

@rsta2 rsta2 closed this as completed May 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0