gcthread.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright (C) 2001-2003 Red Hat, Inc.
  5. *
  6. * Created by David Woodhouse <dwmw2@redhat.com>
  7. *
  8. * For licensing information, see the file 'LICENCE' in this directory.
  9. *
  10. * $Id: gcthread.c,v 1.3 2005/01/22 16:01:12 lunn Exp $
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include "nodelist.h"
  15. //#include <cyg/kernel/kapi.h> prife
  16. #if defined(CYGOPT_FS_JFFS2_GCTHREAD)
  17. #define GC_THREAD_FLAG_TRIG 1
  18. #define GC_THREAD_FLAG_STOP 2
  19. #define GC_THREAD_FLAG_HAS_EXIT 4
  20. #if 0
  21. static cyg_thread_entry_t jffs2_garbage_collect_thread;
  22. void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
  23. {
  24. struct super_block *sb=OFNI_BS_2SFFJ(c);
  25. /* Wake up the thread */
  26. D1(printk("jffs2_garbage_collect_trigger\n"));
  27. cyg_flag_setbits(&sb->s_gc_thread_flags,GC_THREAD_FLAG_TRIG);
  28. }
  29. void
  30. jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
  31. {
  32. struct super_block *sb=OFNI_BS_2SFFJ(c);
  33. RT_ASSERT(c);
  34. RT_ASSERT(!sb->s_gc_thread_handle);
  35. cyg_flag_init(&sb->s_gc_thread_flags);
  36. cyg_mutex_init(&sb->s_lock);
  37. D1(printk("jffs2_start_garbage_collect_thread\n"));
  38. /* Start the thread. Doesn't matter if it fails -- it's only an
  39. * optimisation anyway */
  40. cyg_thread_create(CYGNUM_JFFS2_GC_THREAD_PRIORITY,
  41. jffs2_garbage_collect_thread,
  42. (unsigned long)c,"jffs2 gc thread",
  43. (void*)sb->s_gc_thread_stack,
  44. sizeof(sb->s_gc_thread_stack),
  45. &sb->s_gc_thread_handle,
  46. &sb->s_gc_thread);
  47. cyg_thread_resume(sb->s_gc_thread_handle);
  48. }
  49. void
  50. jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c)
  51. {
  52. struct super_block *sb=OFNI_BS_2SFFJ(c);
  53. RT_ASSERT(sb->s_gc_thread_handle);
  54. D1(printk("jffs2_stop_garbage_collect_thread\n"));
  55. /* Stop the thread and wait for it if necessary */
  56. cyg_flag_setbits(&sb->s_gc_thread_flags,GC_THREAD_FLAG_STOP);
  57. D1(printk("jffs2_stop_garbage_collect_thread wait\n"));
  58. cyg_flag_wait(&sb->s_gc_thread_flags,
  59. GC_THREAD_FLAG_HAS_EXIT,
  60. CYG_FLAG_WAITMODE_OR| CYG_FLAG_WAITMODE_CLR);
  61. // Kill and free the resources ... this is safe due to the flag
  62. // from the thread.
  63. cyg_thread_kill(sb->s_gc_thread_handle);
  64. cyg_thread_delete(sb->s_gc_thread_handle);
  65. cyg_mutex_destroy(&sb->s_lock);
  66. cyg_flag_destroy(&sb->s_gc_thread_flags);
  67. }
  68. static void
  69. jffs2_garbage_collect_thread(unsigned long data)
  70. {
  71. struct jffs2_sb_info *c=(struct jffs2_sb_info *)data;
  72. struct super_block *sb=OFNI_BS_2SFFJ(c);
  73. cyg_flag_value_t flag;
  74. cyg_mtab_entry *mte;
  75. D1(printk("jffs2_garbage_collect_thread START\n"));
  76. while(1) {
  77. flag=cyg_flag_timed_wait(&sb->s_gc_thread_flags,
  78. GC_THREAD_FLAG_TRIG|GC_THREAD_FLAG_STOP,
  79. CYG_FLAG_WAITMODE_OR| CYG_FLAG_WAITMODE_CLR,
  80. cyg_current_time()+
  81. CYGNUM_JFFS2_GS_THREAD_TICKS);
  82. if (flag & GC_THREAD_FLAG_STOP)
  83. break;
  84. D1(printk("jffs2: GC THREAD GC BEGIN\n"));
  85. mte=cyg_fs_root_lookup((cyg_dir *) sb->s_root);
  86. CYG_ASSERT(mte, "Bad mount point");
  87. cyg_fs_lock(mte, mte->fs->syncmode);
  88. if (jffs2_garbage_collect_pass(c) == -ENOSPC) {
  89. printf("No space for garbage collection. "
  90. "Aborting JFFS2 GC thread\n");
  91. break;
  92. }
  93. cyg_fs_unlock(mte, mte->fs->syncmode);
  94. D1(printk("jffs2: GC THREAD GC END\n"));
  95. }
  96. D1(printk("jffs2_garbage_collect_thread EXIT\n"));
  97. cyg_flag_setbits(&sb->s_gc_thread_flags,GC_THREAD_FLAG_HAS_EXIT);
  98. }
  99. #endif
  100. rt_uint32_t cyg_current_time(void)
  101. {
  102. return 0;
  103. }
  104. static void
  105. jffs2_garbage_collect_thread(unsigned long data);
  106. void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
  107. {
  108. struct super_block *sb=OFNI_BS_2SFFJ(c);
  109. /* Wake up the thread */
  110. D1(printk("jffs2_garbage_collect_trigger\n"));
  111. rt_event_send(&sb->s_gc_thread_flags,GC_THREAD_FLAG_TRIG);
  112. }
  113. static struct rt_thread gc_thread;
  114. void
  115. jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
  116. {
  117. struct super_block *sb=OFNI_BS_2SFFJ(c);
  118. cyg_mtab_entry *mte;
  119. int result;
  120. RT_ASSERT(c);
  121. //RT_ASSERT(!sb->s_gc_thread_handle);
  122. mte=(cyg_dir *) sb->s_root;
  123. RT_ASSERT(mte);
  124. rt_event_init(&sb->s_gc_thread_flags, "gc_event", RT_IPC_FLAG_FIFO);
  125. rt_mutex_init(&sb->s_lock, "gc_mutex", RT_IPC_FLAG_FIFO);
  126. // rt_mutex_init(&mte->fs->syncmode, "fs_lock", RT_IPC_FLAG_FIFO);
  127. D1(printk("jffs2_start_garbage_collect_thread\n"));
  128. /* Start the thread. Doesn't matter if it fails -- it's only an
  129. * optimisation anyway */
  130. result = rt_thread_init(&sb->s_gc_thread,
  131. "jffs2_gc_thread",
  132. jffs2_garbage_collect_thread,
  133. (void *)c,
  134. (void*)sb->s_gc_thread_stack,
  135. sizeof(sb->s_gc_thread_stack),
  136. CYGNUM_JFFS2_GC_THREAD_PRIORITY,
  137. CYGNUM_JFFS2_GC_THREAD_TICKS
  138. );
  139. if (result != RT_EOK) {
  140. rt_thread_startup(&sb->s_gc_thread);
  141. /* how to deal with the following filed? */
  142. /* sb->s_gc_thread_handle; */
  143. }
  144. }
  145. void
  146. jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c)
  147. {
  148. struct super_block *sb=OFNI_BS_2SFFJ(c);
  149. cyg_mtab_entry *mte;
  150. rt_uint32_t e;
  151. //RT_ASSERT(sb->s_gc_thread_handle);
  152. D1(printk("jffs2_stop_garbage_collect_thread\n"));
  153. /* Stop the thread and wait for it if necessary */
  154. rt_event_send(&sb->s_gc_thread_flags,GC_THREAD_FLAG_STOP);
  155. D1(printk("jffs2_stop_garbage_collect_thread wait\n"));
  156. rt_event_recv(&sb->s_gc_thread_flags,
  157. GC_THREAD_FLAG_HAS_EXIT,
  158. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  159. RT_WAITING_FOREVER, &e);
  160. // Kill and free the resources ... this is safe due to the flag
  161. // from the thread.
  162. rt_thread_detach(&sb->s_gc_thread);
  163. rt_sem_detach(&sb->s_lock);
  164. rt_event_detach(&sb->s_gc_thread_flags);
  165. }
  166. static void
  167. jffs2_garbage_collect_thread(unsigned long data)
  168. {
  169. struct jffs2_sb_info *c=(struct jffs2_sb_info *)data;
  170. struct super_block *sb=OFNI_BS_2SFFJ(c);
  171. cyg_mtab_entry *mte;
  172. rt_uint32_t flag = 0;
  173. D1(printk("jffs2_garbage_collect_thread START\n"));
  174. while(1) {
  175. rt_event_recv(&sb->s_gc_thread_flags,
  176. GC_THREAD_FLAG_TRIG | GC_THREAD_FLAG_STOP,
  177. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  178. cyg_current_time() + CYGNUM_JFFS2_GS_THREAD_TICKS,
  179. &flag);
  180. if (flag & GC_THREAD_FLAG_STOP)
  181. break;
  182. D1(printk("jffs2: GC THREAD GC BEGIN\n"));
  183. mte=(cyg_dir *) sb->s_root;
  184. RT_ASSERT(mte != NULL);
  185. // rt_mutex_take(&mte->fs->syncmode, RT_WAITING_FOREVER);
  186. if (jffs2_garbage_collect_pass(c) == -ENOSPC) {
  187. printf("No space for garbage collection. "
  188. "Aborting JFFS2 GC thread\n");
  189. break;
  190. }
  191. // rt_mutex_release(&mte->fs->syncmode);
  192. D1(printk("jffs2: GC THREAD GC END\n"));
  193. }
  194. D1(printk("jffs2_garbage_collect_thread EXIT\n"));
  195. rt_event_send(&sb->s_gc_thread_flags,GC_THREAD_FLAG_HAS_EXIT);
  196. }
  197. #endif