trunk/src/emu/cothread.c
r17415 | r17416 | |
1 | | /*************************************************************************** |
2 | | |
3 | | cothread.c |
4 | | |
5 | | Class wrapper around byuu's cothread library. |
6 | | |
7 | | **************************************************************************** |
8 | | |
9 | | Copyright Aaron Giles |
10 | | All rights reserved. |
11 | | |
12 | | Redistribution and use in source and binary forms, with or without |
13 | | modification, are permitted provided that the following conditions are |
14 | | met: |
15 | | |
16 | | * Redistributions of source code must retain the above copyright |
17 | | notice, this list of conditions and the following disclaimer. |
18 | | * Redistributions in binary form must reproduce the above copyright |
19 | | notice, this list of conditions and the following disclaimer in |
20 | | the documentation and/or other materials provided with the |
21 | | distribution. |
22 | | * Neither the name 'MAME' nor the names of its contributors may be |
23 | | used to endorse or promote products derived from this software |
24 | | without specific prior written permission. |
25 | | |
26 | | THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR |
27 | | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
28 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
29 | | DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT, |
30 | | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
31 | | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
32 | | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
33 | | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
34 | | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
35 | | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
36 | | POSSIBILITY OF SUCH DAMAGE. |
37 | | |
38 | | ***************************************************************************/ |
39 | | |
40 | | #include "emu.h" |
41 | | #include "cothread/libco.h" |
42 | | |
43 | | |
44 | | //************************************************************************** |
45 | | // GLOBAL VARIABLES |
46 | | //************************************************************************** |
47 | | |
48 | | // static members of the cothread class |
49 | | osd_lock *cothread::s_create_lock = osd_lock_alloc(); |
50 | | cothread *cothread::s_create_cothread; |
51 | | |
52 | | |
53 | | |
54 | | //************************************************************************** |
55 | | // COTHREAD |
56 | | //************************************************************************** |
57 | | |
58 | | //------------------------------------------------- |
59 | | // cothread - constructor |
60 | | //------------------------------------------------- |
61 | | |
62 | | cothread::cothread(cothread_t existing_thread) |
63 | | : m_cothread(existing_thread), |
64 | | m_creator_cothread(NULL) |
65 | | { |
66 | | } |
67 | | |
68 | | cothread::cothread(cothread_entry_delegate entry, size_t stack) |
69 | | : m_cothread(NULL), |
70 | | m_creator_cothread(co_active()), |
71 | | m_entry(entry) |
72 | | { |
73 | | // due to the lack of input parameter to the entry function, |
74 | | // all cothread creation is explicitly serialized |
75 | | osd_lock_acquire(s_create_lock); |
76 | | s_create_cothread = this; |
77 | | m_cothread = co_create(stack, &cothread_entry); |
78 | | co_switch(m_cothread); |
79 | | osd_lock_release(s_create_lock); |
80 | | } |
81 | | |
82 | | |
83 | | //------------------------------------------------- |
84 | | // cothread_entry - static entry point |
85 | | //------------------------------------------------- |
86 | | |
87 | | void cothread::cothread_entry() |
88 | | { |
89 | | // on first call, retrieve the static pointer and return |
90 | | cothread *thread = s_create_cothread; |
91 | | co_switch(thread->m_creator_cothread); |
92 | | |
93 | | // when actually swapped in later, call the entry point |
94 | | thread->m_entry(); |
95 | | } |
trunk/src/emu/cothread.h
r17415 | r17416 | |
1 | | /*************************************************************************** |
2 | | |
3 | | cothread.h |
4 | | |
5 | | Class wrapper around byuu's cothread library. |
6 | | |
7 | | **************************************************************************** |
8 | | |
9 | | Copyright Aaron Giles |
10 | | All rights reserved. |
11 | | |
12 | | Redistribution and use in source and binary forms, with or without |
13 | | modification, are permitted provided that the following conditions are |
14 | | met: |
15 | | |
16 | | * Redistributions of source code must retain the above copyright |
17 | | notice, this list of conditions and the following disclaimer. |
18 | | * Redistributions in binary form must reproduce the above copyright |
19 | | notice, this list of conditions and the following disclaimer in |
20 | | the documentation and/or other materials provided with the |
21 | | distribution. |
22 | | * Neither the name 'MAME' nor the names of its contributors may be |
23 | | used to endorse or promote products derived from this software |
24 | | without specific prior written permission. |
25 | | |
26 | | THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR |
27 | | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
28 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
29 | | DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT, |
30 | | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
31 | | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
32 | | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
33 | | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
34 | | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
35 | | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
36 | | POSSIBILITY OF SUCH DAMAGE. |
37 | | |
38 | | ***************************************************************************/ |
39 | | |
40 | | #pragma once |
41 | | |
42 | | #ifndef __EMU_H__ |
43 | | #error Dont include this file directly; include emu.h instead. |
44 | | #endif |
45 | | |
46 | | #ifndef __COTHREAD_H__ |
47 | | #define __COTHREAD_H__ |
48 | | |
49 | | #include "cothread/libco.h" |
50 | | |
51 | | |
52 | | //************************************************************************** |
53 | | // TYPE DEFINITIONS |
54 | | //************************************************************************** |
55 | | |
56 | | // delegate that points to a thread's entry |
57 | | typedef delegate<void ()> cothread_entry_delegate; |
58 | | |
59 | | |
60 | | // ======================> cothread |
61 | | |
62 | | class cothread |
63 | | { |
64 | | public: |
65 | | // construction/destruction |
66 | | cothread(cothread_t existing_thread); |
67 | | cothread(cothread_entry_delegate entry, size_t stack = 1048576); |
68 | | ~cothread() { if (m_creator_cothread != NULL) co_delete(m_cothread); } |
69 | | |
70 | | // switching |
71 | | void make_active() { co_switch(m_cothread); } |
72 | | |
73 | | private: |
74 | | // internal helpers |
75 | | static void cothread_entry(); |
76 | | |
77 | | // internal state |
78 | | cothread_t m_cothread; |
79 | | cothread_t m_creator_cothread; |
80 | | cothread_entry_delegate m_entry; |
81 | | |
82 | | // static state |
83 | | static osd_lock * s_create_lock; |
84 | | static cothread * s_create_cothread; |
85 | | }; |
86 | | |
87 | | |
88 | | #endif // __COTHREAD_H__ |
trunk/src/emu/diexec.c
r17415 | r17416 | |
72 | 72 | |
73 | 73 | device_execute_interface::device_execute_interface(const machine_config &mconfig, device_t &device) |
74 | 74 | : device_interface(device), |
75 | | // m_cothread(cothread_entry_delegate(FUNC(device_execute_interface::run_thread_wrapper), this)), |
76 | 75 | m_disabled(false), |
77 | 76 | m_vblank_interrupt_legacy(NULL), |
78 | 77 | m_vblank_interrupt_screen(NULL), |
r17415 | r17416 | |
388 | 387 | |
389 | 388 | |
390 | 389 | //------------------------------------------------- |
391 | | // run_thread_wrapper - wrapper for our cothread |
392 | | // which just calls run and then returns to the |
393 | | // scheduler thread, over and over |
394 | | //------------------------------------------------- |
395 | | /* |
396 | | void device_execute_interface::run_thread_wrapper() |
397 | | { |
398 | | // loop infinitely |
399 | | device_scheduler &scheduler = device().machine().scheduler(); |
400 | | while (1) |
401 | | { |
402 | | // call the classic run function, then swap back to the scheduler's thread |
403 | | execute_run(); |
404 | | scheduler.make_active(); |
405 | | } |
406 | | } |
407 | | */ |
408 | | |
409 | | //------------------------------------------------- |
410 | 390 | // execute_clocks_to_cycles - convert the number |
411 | 391 | // of clocks to cycles, rounding down if necessary |
412 | 392 | //------------------------------------------------- |