Win32 thread-safety

View: New views
3 Messages — Rating Filter:   Alert me  

Win32 thread-safety

by sgoericke :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

i use cairo (or better: cairomm) for a X-Plane aircraft addon, meaning
it's used in a DLL/.so/.dylib. For text i use(d) the simple "toy-text"
API because it do a good job for my needs. The plugin run fine on Linux
(i use mainly Linux for development and daily use) and also runs fine on
MacOS. Linux-Distribution is Archlinux, OSX Version is still 10.5.8. But
on Windows i come into trouble: using MinGW/GDB i got error messages
from the Microsoft MSVC Runtime-Library "this application has called a
runtime function in a unusual way" or similar. However, GDB keeps the
app running, couldn't tll me what's going wrong with the app bevor i
click "Ok" on the error message-box. Thereafter gdb told me there was a
"std::bad_alloc" exception but couldn't tell me were. So i checked every
"new" etc. in my code, also add a lot "try-catch" statements around them
but nothing helps.

To get closer to the problem i decided to compile and debug the stuff
with MSVC (Visual Studio 2008). I found out the "bad_alloc" exception
was thrown by the cairomm-library (CAIRO_STATUS_NO_MEMORY). But i still
didn't have an idea why. Because the errors are "random" ones it was
hard to find the problem. The first hint i got was that the exception
always was thrown if i did anything with text (get_text_extents(),
show_text() mainly).  A hour ago i just found that one of my
drawing-threads has thrown the exception  and another  called  
"cairo_show_text()" the same time.  So i came to the conclusion  that
there *must* be a probelm with text-handling on Win32 systems. Btw., at
the moment i run up to 8 threads that use cairo and growing.

So i tried a "simple" approach and add a global Mutex to my plugin and
cover every text-related stuff in my code by this Mutex. And surprise
surprise, this helps. But also reduces Performance a bit in the
Application, saying X-Plane.

I also don't longer use "normal" Win32-Font objects and
"set_font_size()" but switched to Win32ScaledFont (which didn't solve
the problem).

So, my question: is cairomm thread-safe on Win32? I mean the Font/Text
stuff ? Second: if so, is it only thread-safe with native Win32 Threads?
I use pthreads2 for Win32 (i'm happy with that).

used cairo-version: 1.8.8
used cairomm-version: 1.8.2
tested OS: Windows Vista 64bit, Windows 7 64bit, Linux 32bit (Kernel
2.6.30 with PAE and Highmem = 64GB, i have 8GB of RAM, Glibc 2.10),
MacOSX 10.5.8

Anyway, thanks for that highly protable vector-graphics library :-)

http://www.sgoericke.de/screenshot_1.png
http://www.youtube.com/watch?v=slVWL0tks9o
_______________________________________________
cairo mailing list
cairo@...
http://lists.cairographics.org/mailman/listinfo/cairo

Re: Win32 thread-safety

by cu-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I also found this to be the case, and all our text output code is
currently wrapped in mutexes.

So I'd be very interested in a fix as well. FWIW I recall having
problems with that code on OSX as well as on Windows, though on Windows
they seem to be more widely spread and more obvious. Incidentally, it
has gotten slightly worse in versions after 1.8.6 (occasionally I try to
disable mutexes and see if the issue was somehow resolved).




Sven Goericke wrote:
> So i tried a "simple" approach and add a global Mutex to my plugin and
> cover every text-related stuff in my code by this Mutex. And surprise
> surprise, this helps. But also reduces Performance a bit in the
> Application, saying X-Plane.
>
> I also don't longer use "normal" Win32-Font objects and
> "set_font_size()" but switched to Win32ScaledFont (which didn't solve
> the problem).
>  

_______________________________________________
cairo mailing list
cairo@...
http://lists.cairographics.org/mailman/listinfo/cairo

Re: Win32 thread-safety

by Adrian Johnson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sven Goericke wrote:

> To get closer to the problem i decided to compile and debug the stuff
> with MSVC (Visual Studio 2008). I found out the "bad_alloc" exception
> was thrown by the cairomm-library (CAIRO_STATUS_NO_MEMORY). But i still
> didn't have an idea why. Because the errors are "random" ones it was
> hard to find the problem. The first hint i got was that the exception
> always was thrown if i did anything with text (get_text_extents(),
> show_text() mainly).  A hour ago i just found that one of my
> drawing-threads has thrown the exception  and another  called  
> "cairo_show_text()" the same time.  So i came to the conclusion  that
> there *must* be a probelm with text-handling on Win32 systems. Btw., at
> the moment i run up to 8 threads that use cairo and growing.
>
> So i tried a "simple" approach and add a global Mutex to my plugin and
> cover every text-related stuff in my code by this Mutex. And surprise
> surprise, this helps. But also reduces Performance a bit in the
> Application, saying X-Plane.

I tested some code based on the pthreads-show-text test and it appears
that _get_global_font_dc () is the problem. Changing the win32 font
backend to make _get_global_font_dc () create a new DC instead of
reusing the same one and freeing the DC at the end of each function that
called _get_global_font_dc () makes my test case work.

Also the documentation for CreateCompatibleDC() states that the thread
that calls CreateCompatibleDC() owns the DC. When the thread is
destroyed the DC is no longer valid.

I'm not sure what the performance impact of creating a new DC each time
is is required. Should the DC be stored in thread local storage?

_______________________________________________
cairo mailing list
cairo@...
http://lists.cairographics.org/mailman/listinfo/cairo