diff --git a/libgcc/config/i386/gthr-win32.c b/libgcc/config/i386/gthr-win32.c
index eec16b3..10769a7 100644
--- a/libgcc/config/i386/gthr-win32.c
+++ b/libgcc/config/i386/gthr-win32.c
@@ -5,6 +5,8 @@
    Contributed by Mumit Khan <khan@xraylith.wisc.edu>.
    Modified and moved to separate file by Danny Smith
    <dannysmith@users.sourceforge.net>.
+   Implemented Condition Variable by Tomohiro Kashiwada
+   <kikairoya@gmail.com>.

 This file is part of GCC.

@@ -28,6 +30,7 @@ see the files COPYING3 and COPYING.RUNTIME
respectively.  If not, see
 <http://www.gnu.org/licenses/>.  */

 #include <windows.h>
+#include <process.h>
 #ifndef __GTHREAD_HIDE_WIN32API
 # define __GTHREAD_HIDE_WIN32API 1
 #endif
@@ -259,9 +262,296 @@ __gthr_win32_recursive_mutex_unlock
(__gthread_recursive_mutex_t *mutex)
   return 0;
 }

+
 int
 __gthr_win32_recursive_mutex_destroy (__gthread_recursive_mutex_t *mutex)
 {
   CloseHandle ((HANDLE) mutex->sema);
   return 0;
 }
+
+
+static DWORD
+gthread_calc_time_offset (const __gthread_time_t *abs_time)
+{
+  FILETIME ft;
+  LONGLONG ll;
+  LONGLONG ret;
+  GetSystemTimeAsFileTime (&ft);
+  ll = ((LONGLONG)ft.dwHighDateTime << 32) + ft.dwLowDateTime;
+  ret = (((LONGLONG)abs_time->tv_sec * 10000000 + abs_time->tv_nsec / 100)
+ - (ll - 116444736000000000)) / 10000;
+  if (ret < 0)
+    return 0;
+  if (ret > 0x7FFFFFFF)
+    return 0x7FFFFFFF;
+  return ret;
+}
+
+int
+__gthr_win32_cond_init (__gthread_cond_t *cond,
+ void *attr __attribute__((unused)))
+{
+  cond->counter = -1;
+  cond->pcs = malloc (sizeof (CRITICAL_SECTION));
+  InitializeCriticalSection ((LPCRITICAL_SECTION)cond->pcs);
+  cond->sema = CreateSemaphore (NULL, 0, 65535, NULL);
+  return 0;
+}
+
+int
+__gthr_win32_cond_destroy (__gthread_cond_t *cond)
+{
+  __gthread_cond_broadcast (cond);
+  free (cond->pcs);
+  CloseHandle ((HANDLE)cond->sema);
+  return 0;
+}
+
+int
+__gthr_win32_cond_broadcast (__gthread_cond_t *cond)
+{
+  EnterCriticalSection (cond->pcs);
+  if (cond->counter >= 0)
+    ReleaseSemaphore (cond->sema, cond->counter+1, NULL);
+  cond->counter = -1;
+  LeaveCriticalSection (cond->pcs);
+  return 0;
+}
+
+static int
+cond_wait_impl (__gthread_cond_t *cond,
+ __gthread_mutex_t *mutex, DWORD timeout)
+{
+  EnterCriticalSection (cond->pcs);
+  ++cond->counter;
+  LeaveCriticalSection (cond->pcs);
+  if (SignalObjectAndWait (mutex->sema, cond->sema, timeout, FALSE)
+      != WAIT_OBJECT_0)
+    {
+      EnterCriticalSection (cond->pcs);
+      --cond->counter;
+      LeaveCriticalSection (cond->pcs);
+    }
+  WaitForSingleObject (mutex->sema, INFINITE);
+  return 0;
+}
+
+static int
+cond_wait_recursive_impl (__gthread_cond_t *cond,
+  __gthread_recursive_mutex_t *mutex, DWORD timeout)
+{
+  EnterCriticalSection (cond->pcs);
+  ++cond->counter;
+  LeaveCriticalSection (cond->pcs);
+  if (SignalObjectAndWait (mutex->sema, cond->sema, timeout, FALSE)
+      != WAIT_OBJECT_0)
+    {
+      EnterCriticalSection (cond->pcs);
+      --cond->counter;
+      LeaveCriticalSection (cond->pcs);
+    }
+  WaitForSingleObject (mutex->sema, INFINITE);
+  return 0;
+}
+
+int
+__gthr_win32_cond_wait (__gthread_cond_t *cond, __gthread_mutex_t *mutex)
+{
+  return cond_wait_impl (cond, mutex, INFINITE);
+}
+
+int
+__gthr_win32_cond_wait_recursive (__gthread_cond_t *cond,
+  __gthread_recursive_mutex_t *mutex)
+{
+  return cond_wait_recursive_impl (cond, mutex, INFINITE);
+}
+
+static unsigned
+get_thread_id (__gthread_t thr)
+{
+  if (thr)
+    return thr->id;
+  return GetCurrentThreadId ();
+}
+
+static DWORD gthr_gthread_t_get_slot (void)
+{
+  static DWORD gthread_slot = 0xFFFFFFFF;
+  if (gthread_slot == 0xFFFFFFFF)
+    {
+      DWORD s = TlsAlloc ();
+      if ((DWORD) __GTHR_W32_InterlockedCompareExchange((long
int*)(&gthread_slot), s, 0xFFFFFFFF)
+  != 0xFFFFFFFFu)
+ {
+  TlsFree (s);
+  return gthread_slot;
+ }
+      else
+ {
+  __gthread_t thr = (__gthread_t)malloc (sizeof (__gthread_solid_t));
+  thr->func = NULL;
+  thr->arg = NULL;
+  thr->id = GetCurrentThreadId ();
+  DuplicateHandle(GetCurrentProcess (), GetCurrentThread (),
+  GetCurrentProcess (), (HANDLE *)&thr->id,
+  0, FALSE, DUPLICATE_SAME_ACCESS);
+  TlsSetValue(gthread_slot, thr);
+ }
+    }
+  return gthread_slot;
+}
+
+static unsigned __stdcall
+gthr_entry_stub (void *arg)
+{
+  __gthread_t p = (__gthread_t)arg;
+  TlsSetValue (gthr_gthread_t_get_slot (), arg);
+  /* the return code of the thread function is ignored as we have
here anyway only
+     a unsigned which is not enough to pass a full pointer.
+  */
+  void *ret=(*p->func) (p->arg);
+  if(ret!=0)
+  {
+    DWORD result=*(DWORD*)(ret);
+    ExitThread(result);
+  }
+  else
+  {
+    ExitThread(0);
+  }
+
+  return 0 ;
+}
+
+
+int
+__gthr_win32_create (__gthread_t *thrp, void *(*func) (void *), void *arg)
+{
+  gthr_gthread_t_get_slot ();
+  *thrp = (__gthread_t)malloc (sizeof (__gthread_solid_t));
+  (*thrp)->func = func;
+  (*thrp)->arg = arg;
+  (*thrp)->handle = (void *)_beginthreadex (NULL, 0, &gthr_entry_stub, *thrp,
+    0, &(*thrp)->id);
+  if((*thrp)->handle == 0)
+  {
+    return -1;
+  }
+
+  return 0;
+}
+
+int
+__gthr_win32_join (__gthread_t thr, void **pv)
+{
+  WaitForSingleObject ((HANDLE)thr->handle, INFINITE);
+
+  if (pv)
+  {
+     LPDWORD r;
+     r=(LPDWORD)*pv;
+     GetExitCodeThread ((HANDLE)thr->handle, r);
+  }
+  __gthr_win32_detach (thr);
+  return 0;
+}
+
+int __gthr_win32_detach (__gthread_t thr)
+{
+  CloseHandle ((HANDLE)thr->handle);
+  free (thr);
+  return 0;
+}
+
+int __gthr_win32_equal (__gthread_t t1, __gthread_t t2)
+{
+  return get_thread_id (t1) == get_thread_id (t2);
+}
+
+__gthread_t
+__gthr_win32_self (void)
+{
+  return (__gthread_t)TlsGetValue (gthr_gthread_t_get_slot ());
+}
+
+int
+__gthr_win32_yield (void)
+{
+  Sleep(0);
+  return 0;
+}
+
+int
+__gthr_win32_mutex_timedlock (__gthread_mutex_t *mutex,
+      const __gthread_time_t *abs_timeout)
+{
+  if (InterlockedIncrement (&mutex->counter) == 0 ||
+      WaitForSingleObject (mutex->sema, gthread_calc_time_offset (abs_timeout))
+      == WAIT_OBJECT_0)
+    return 0;
+  else
+    {
+      InterlockedDecrement (&mutex->counter);
+      return 0;
+    }
+}
+
+int
+__gthr_win32_recursive_mutex_timedlock (__gthread_recursive_mutex_t *mutex,
+ const __gthread_time_t *abs_timeout)
+{
+  DWORD me = GetCurrentThreadId ();
+  if (InterlockedIncrement (&mutex->counter) == 0)
+    {
+      mutex->depth = 1;
+      mutex->owner = me;
+    }
+  else if (mutex->owner == me)
+    {
+      InterlockedDecrement (&mutex->counter);
+      ++(mutex->depth);
+    }
+  else if (WaitForSingleObject (mutex->sema,
+ gthread_calc_time_offset (abs_timeout))
+   == WAIT_OBJECT_0)
+    {
+      mutex->depth = 1;
+      mutex->owner = me;
+    }
+  else
+    {
+      InterlockedDecrement (&mutex->counter);
+    }
+  return 0;
+}
+
+int
+__gthr_win32_cond_signal (__gthread_cond_t *cond)
+{
+  EnterCriticalSection (cond->pcs);
+  if (cond->counter >= 0)
+    {
+      --cond->counter;
+      ReleaseSemaphore (cond->sema, 1, NULL);
+    }
+  LeaveCriticalSection (cond->pcs);
+  return 0;
+}
+
+int
+__gthr_win32_cond_timedwait (__gthread_cond_t *cond, __gthread_mutex_t *mutex,
+     const __gthread_time_t *abs_timeout)
+{
+  return cond_wait_impl (cond, mutex, gthread_calc_time_offset (abs_timeout));
+}
+
+int
+__gthr_win32_cond_timedwait_recursive (__gthread_cond_t *cond,
+       __gthread_recursive_mutex_t *mutex,
+       const __gthread_time_t *abs_timeout)
+{
+  return cond_wait_recursive_impl (cond, mutex,
+   gthread_calc_time_offset (abs_timeout));
+}
diff --git a/libgcc/config/i386/gthr-win32.h b/libgcc/config/i386/gthr-win32.h
index c295fd5..af1f7cc 100644
--- a/libgcc/config/i386/gthr-win32.h
+++ b/libgcc/config/i386/gthr-win32.h
@@ -3,6 +3,8 @@

 /* Copyright (C) 1999-2014 Free Software Foundation, Inc.
    Contributed by Mumit Khan <khan@xraylith.wisc.edu>.
+   Implemented Condition Variable by Tomohiro Kashiwada
+   <kikairoya@gmail.com>.

 This file is part of GCC.

@@ -29,8 +31,13 @@ see the files COPYING3 and COPYING.RUNTIME
respectively.  If not, see
 #define GCC_GTHR_WIN32_H

 /* Make sure CONST_CAST2 (origin in system.h) is declared.  */
+#undef CONST_CAST2
 #ifndef CONST_CAST2
-#define CONST_CAST2(TOTYPE,FROMTYPE,X) ((__extension__(union
{FROMTYPE _q; TOTYPE _nq;})(X))._nq)
+#ifdef __cplusplus
+#define CONST_CAST2(TOTYPE,FROMTYPE,X) (const_cast<TOTYPE> (X))
+#else
+#define CONST_CAST2(TOTYPE,FROMTYPE,X) ((__extension__(union
{FROMTYPE _q; TOTYPE _nqtwi;})(X))._nqtwi)
+#endif
